Practice
parkVehicle() method is not synchronized. Which exact sequence of Thread 1 and Thread 2 calls causes double-booking of the same parking spot?Solution
Step 1: Identify check-then-act race condition
Without synchronization, Thread 1 reads spot as available but before marking it occupied, context switches to Thread 2 which also reads available.Step 2: Both threads proceed to park, causing double-booking
Both threads act on stale availability info, violating mutual exclusion.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
WeekendFeeStrategy for weekend bookings in the Airline Reservation System, which minimal change respects SOLID principles?Solution
Step 1: Identify SOLID principle involved
The Open-Closed Principle (OCP) requires that existing classes remain unchanged when adding new behavior.Step 2: Evaluate options for OCP compliance
Add a boolean flagisWeekendto theBookingclass and handle fees there. adds a flag and handles fees in Booking, violating SRP and mixing concerns. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without changing existing classes. cleanly adds a new strategy class without modifying existing code, respecting OCP and DIP.Final Answer:
Option B -> Option BQuick Check:
New FeeStrategy class -> no existing class modification -> OCP respected [check]
castVote() method is not synchronized, which exact sequence of thread interleaving causes a double vote count for the same user on a question?Solution
Step 1: Identify the check-then-act race condition
The critical issue is the check if the user has already voted. Without synchronization, both threads see no vote recorded and proceed.Step 2: Trace exact interleaving causing double vote
Thread 1 checks voted -> false, context switch; Thread 2 checks voted -> false; both cast votes, causing double count.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double vote [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new classes for ElectricCar and ChargingSpot allows extension without changing existing class code, preserving OCP.Step 2: Why updating compatibility map is needed
SpotCompatibility.COMPATIBILITY_MAP controls which vehicles fit which spots; updating it ensures ElectricCar and ChargingSpot compatibility without modifying base classes.Final Answer:
Option B -> Option BQuick Check:
New enums + classes + compatibility map update -> minimal existing class changes [check]
VehicleType and SpotType enums, and SpotCompatibility map, to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot requires new enum values and a new class without modifying existing classes to respect OCP.Step 2: Apply minimal change with enums and composition
Adding new enum constants and extendingVehiclewithElectricCarallows reuse of existing logic. UpdatingSpotCompatibility.COMPATIBILITY_MAPwith new mappings supports compatibility without changing existing code.Step 3: Eliminate options violating OCP
Options B, C, and D modify existing classes or require widespread changes, violating OCP and risking regressions.Final Answer:
Option B -> Option BQuick Check:
New enums + subclass + compatibility map update -> minimal, OCP-compliant [check]
