Practice
Command interface declares an execute() method and concrete commands implement this interface to encapsulate operations. Which OOP concept is primarily applied here and why is this the correct use case?Solution
Step 1: Identify the role of Command objects
Each command encapsulates all information needed to perform an action, hiding the operation's details from the invoker.Step 2: Differentiate encapsulation from other concepts
While polymorphism and abstraction are involved, the key concept is encapsulation because commands package the action and its parameters as an object.Final Answer:
Option A -> Option AQuick Check:
Command objects encapsulate operations and data for undo/redo [check]
TrafficLightController class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct choice?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Why Singleton fits traffic light controller
Only one controller should coordinate the traffic lights to avoid conflicting states; multiple instances would cause inconsistent signals.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
WeekendFeeStrategy to calculate plugin usage fees on weekends without modifying existing plugin classes. Which SOLID principle guides the minimum change, and what is the correct approach?Solution
Step 1: Identify the SOLID principle for adding new behavior without modifying existing code
The Open-Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP by creating a new WeekendFeeStrategy implementing FeeStrategy interface
This allows adding weekend fee logic without changing existing plugin or fee classes.Final Answer:
Option A -> Option AQuick Check:
OCP: extend with new strategy class, no existing class changes [check]
WeekendFeeStrategy to the rate limiter's fee calculation without modifying existing classes. Which SOLID principle applies and what is the minimum change?Solution
Step 1: Identify SOLID principle for adding new fee logic
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options for minimal change respecting OCP and DIP
Add weekend flag to RateLimiter and use if-else inside existing fee method adds weekend flag and if-else inside existing method, violating OCP by modifying existing class. Create new WeekendFeeStrategy implementing FeeStrategy interface; inject it via constructor (OCP and DIP enforced) respects OCP and DIP by adding new class and injecting it.Final Answer:
Option A -> Option AQuick Check:
Minimal change but violates OCP by modifying existing class [check]
FriendRequestManager class has a sendRequest() method that checks if a friend request exists before creating one. Without synchronization, which exact thread interleaving causes duplicate friend requests?Solution
Step 1: Identify the check-then-act race condition
Both threads check for existing request before creation; without synchronization, both see none.Step 2: Trace thread interleaving
Thread 1 checks and pauses before creating; Thread 2 checks and proceeds; both create duplicate requests.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> duplicate friend requests [check]
