Practice
Executor class's executeStep() method is not synchronized, which exact thread interleaving causes two threads to execute the same step concurrently, leading to inconsistent state?Solution
Step 1: Identify the check-then-act race
Without synchronization, Thread 1 checks step status as free, then context switches before marking running.Step 2: Thread 2 repeats check before Thread 1 marks running
Thread 2 also sees step free and proceeds, causing both to execute concurrently.Final Answer:
Option C -> Option CQuick Check:
Unsynchronized check-then-act -> double execution race [check]
placeBid() method checks if the auction is still open before accepting a bid. Without synchronizing placeBid(), which exact thread interleaving causes two threads to accept bids after the auction closes, resulting in inconsistent state?Solution
Step 1: Identify check-then-act race
Without synchronization, Thread 1 reads auction open as true but is preempted before recording the bid.Step 2: Thread 2 reads auction open as true and proceeds to record bid as well, both bypassing the closed state check.
Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double acceptance after close [check]
ElectricCar and ChargingSpot without modifying existing classes. What is the minimum change to the current Java design to add these features while preserving extensibility?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enums or modifying compatibility maps changes existing classes, violating OCP.Step 2: Use composition by adding an isElectric field and charging logic in ParkingSpot to handle charging without class explosion.
This approach extends functionality with minimal changes and avoids class explosion or god classes.Final Answer:
Option B -> Option BQuick Check:
Use fields and composition, avoid modifying enums or compatibility maps [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Enums VehicleType and SpotType can be extended with new constants without changing existing class logic. Creating ElectricCar subclass extends Vehicle cleanly.Step 2: Update SpotCompatibility map externally
Maintaining compatibility mapping outside existing classes avoids modifying them, preserving OCP and avoiding class explosion.Final Answer:
Option A -> Option AQuick Check:
Extend enums + subclass + external compatibility map -> minimal change [check]
Solution
Step 1: Identify extensibility requirements
Supporting ElectricCar and ChargingSpots requires new types without changing existing class logic to respect OCP.Step 2: Apply minimal change approach
Adding new enum values and subclass ElectricCar allows extension without modifying existing classes. Updating SpotCompatibility map centralizes compatibility logic.Step 3: Eliminate options violating OCP
Options B, C, and D modify existing classes or add conditional logic, violating OCP and risking class explosion or god classes.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + compatibility map update -> minimal, OCP-compliant [check]
