Practice
Solution
Step 1: Understand Handler role
Handlers define a contract to process or pass requests; they typically do not share mutable state.Step 2: Evaluate Java features
Interfaces allow multiple inheritance and default methods (since Java 8), promoting flexibility; abstract classes share state but restrict inheritance.Final Answer:
Option A -> Option AQuick Check:
Interface defines contract without forcing shared state [check]
loadPlugin() method checks if a plugin is already loaded before loading it. Without synchronizing this method, what exact thread interleaving causes the same plugin to be loaded twice?Solution
Step 1: Trace the check-then-act sequence without synchronization
Thread 1 calls isLoaded() and sees false, then context switches before loading.Step 2: Thread 2 also calls isLoaded() and sees false, both threads proceed to load plugin concurrently, causing duplicate loading.
Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act allows double loading due to race [check]
WeekendBonusRule that gives players extra moves on weekends without modifying existing classes. Which SOLID principle guides the minimal change, and what is the correct approach?Solution
Step 1: Identify SOLID principle for extension without modification
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Evaluate approaches
Subclassing Player to WeekendPlayer allows extension without modifying Player; injecting new MoveRule class also respects OCP but requires interface design.Final Answer:
Option C -> Option CQuick Check:
Subclassing extends behavior without modifying base class [check]
WeekendFeeStrategy to the parking lot pricing requires which minimum change to comply with SOLID principles?Solution
Step 1: Identify the SOLID principle involved
The Open/Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Evaluate options against OCP
Add a boolean flagisWeekendtoParkingLotand handle fees internally. adds weekend logic insideParkingLot, modifying existing code and violating OCP. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without modifying existing classes. creates a new strategy class and injects it, adhering to OCP. Modify the existingFeeStrategyclass to add weekend logic with an if-statement. modifiesFeeStrategydirectly, violating OCP. ExtendParkingLotwith a subclassWeekendParkingLotthat overrides fee calculation. subclassesParkingLot, which can cause class explosion and violates single responsibility.Final Answer:
Option A -> Option AQuick Check:
Modifying ParkingLot internals violates OCP -> not minimal change [check]
ElectricCar vehicles that require ChargingSpot notifications. What is the minimum design change to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
New vehicle and spot types must be added without modifying existing classes to respect OCP.Step 2: Apply minimal changes
Adding new enum values and creating new subclasses allows extension without modifying existing class code; updating compatibility map centralizes new logic.Step 3: Eliminate other options
Options B, C, and D modify existing classes or factory logic, violating OCP and risking class explosion or god-class anti-pattern.Final Answer:
Option C -> Option CQuick Check:
New enums and subclasses extend design without modifying existing classes [check]
