Practice
1. In designing the Vehicle abstraction for the Car Rental System, should
Vehicle be an abstract class or an interface, and why?easy
Solution
Step 1: Identify Vehicle's shared state and behavior
Vehicle has common fields likelicensePlateand methods with shared logic such ascalculateRentalPrice(), which require state and partial implementation.Step 2: Determine appropriate abstraction
Abstract classes allow shared state and method implementations, while interfaces cannot hold instance state. Thus, Vehicle should be an abstract class.Final Answer:
Option C -> Option CQuick Check:
Shared state + common methods -> abstract class [check]
Hint: Shared state? Use abstract class, not interface [check]
Common Mistakes:
2. In the Hotel Management System, if the
parkVehicle() method is not synchronized, which exact sequence of events between Thread 1 and Thread 2 causes double-booking of the same parking spot?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available and is preempted before marking it occupied.Step 2: Thread 2 also reads spot as available and proceeds to park, causing double-booking.
This is the classic check-then-act gap causing race condition.Final Answer:
Option A -> Option AQuick Check:
Check availability -> true by both threads before marking occupied -> double-booking [check]
Hint: Check-then-act gap -> double-booking without synchronization [check]
Common Mistakes:
3. In a concurrent Job Scheduler, if the
scheduleJob() method is not synchronized, which exact thread interleaving causes the same Job to be scheduled twice?medium
Solution
Step 1: Identify the check-then-act race condition
Both threads check if the job is already scheduled before adding. Without synchronization, both see job absent.Step 2: Trace thread interleaving
Thread 1 checks -> false, context switch before adding; Thread 2 checks -> false, adds job; Thread 1 resumes and adds job again, causing double scheduling.Final Answer:
Option C -> Option CQuick Check:
Check-then-act gap without synchronization -> double scheduling [check]
Hint: Check-then-act gap -> double scheduling race [check]
Common Mistakes:
4. Adding a
WeekendFeeStrategy to the parking lot pricing requires which minimum change to comply with SOLID principles?medium
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
Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without modifying existing classes. respects OCP by adding a new strategy class without modifying existing code. Add a booleanisWeekendflag toVehicleand change pricing logic accordingly. violates OCP by modifyingVehiclestate, which is unrelated to pricing and causes tight coupling.Final Answer:
Option B -> Option BQuick Check:
New strategy class -> OCP-compliant extension [check]
Hint: OCP: add new class, don't modify existing ones [check]
Common Mistakes:
5. To support
ElectricCar bidders requiring ChargingSpot reservations in the auction system, what is the minimum change to the current Java design to maintain extensibility without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modification
Adding new enum values and subclasses extends functionality without changing existing class code.Step 2: Update SpotCompatibility map externally to maintain compatibility rules without modifying existing classes.
Final Answer:
Option B -> Option BQuick Check:
Extend enums + subclass + external map update -> minimal change [check]
Hint: Extend enums + subclass + external map update -> minimal change [check]
Common Mistakes:
