Practice
1. In the Food Ordering System, the
OrderManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct choice?easy
Solution
Step 1: Identify the pattern from constructor and method signature
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance is created and accessed globally.Step 2: Why Singleton fits this use case
OrderManager must coordinate all orders centrally to avoid conflicting states; Singleton guarantees a single shared instance.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
2. In designing the Airline Reservation System, should the
Seat entity be modeled as an abstract class or an interface, and why?easy
Solution
Step 1: Analyze seat characteristics
Seats have shared state (seat number, class) and common behavior (availability, booking), which suggests code reuse.Step 2: Abstract class vs interface
Abstract class allows shared state and default behavior implementation, while interface cannot hold state and only defines method signatures.Final Answer:
Option A -> Option AQuick Check:
Shared state + behavior -> abstract class [check]
Hint: Shared state needs abstract class, interfaces lack fields [check]
Common Mistakes:
3. To add a new
WeekendFeeStrategy pricing rule in the Workflow Engine's fee calculation, which minimal change respects SOLID principles?medium
Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Apply OCP to fee strategy
Adding a new WeekendFeeStrategy class extends behavior without modifying existing FeeCalculator, respecting OCP and Dependency Inversion Principle.Final Answer:
Option B -> Option BQuick Check:
New strategy class + no FeeCalculator change -> OCP respected [check]
Hint: OCP: extend with new class, don't modify existing [check]
Common Mistakes:
4. In a parking lot system using the Strategy pattern, if
parkVehicle() is NOT synchronized, which exact thread interleaving causes double-booking of the same spot?medium
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
Because the state update is not atomic, both threads believe the spot is free and park simultaneously.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap -> double-booking due to unsynchronized parkVehicle() [check]
Hint: Check availability -> context switch -> both park -> double-booking [check]
Common Mistakes:
5. The system needs to support
ElectricCar and ChargingSpot without modifying existing Vehicle or Spot classes. What is the minimum change to support this extensibility in Java?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Enums can be extended with new constants; subclassingElectricCaradds new behavior without changingVehicle.Step 2: Update compatibility externally
UpdatingSpotCompatibility.COMPATIBILITY_MAPexternally respects OCP and avoids class explosion or god classes.Final Answer:
Option B -> Option BQuick Check:
Extend enums + subclass + external map update -> minimal change [check]
Hint: Extend enums + subclass + external map -> minimal change [check]
Common Mistakes:
