Practice
1. In the Hotel Management System, the
RoomManager 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
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance, which matches the Singleton pattern.Step 2: Why Singleton fits RoomManager
RoomManager manages shared state like room availability; having multiple instances risks inconsistent state. Singleton ensures centralized control.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized static getInstance() -> Singleton [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
2. In the Stack Overflow voting system, if the
castVote() method is not synchronized, which exact sequence of thread interleaving causes a double vote count for the same user on a question?medium
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]
Hint: Check-then-act gap -> double vote without sync [check]
Common Mistakes:
3. In a multi-threaded parking lot system, if
parkVehicle() is NOT synchronized, which exact sequence causes double-booking of the same spot?medium
Solution
Step 1: Identify the race condition
Without synchronization, both threads can read spot as available before either marks it occupied.Step 2: Trace exact interleaving
Thread 1: isAvailable() -> true, context switch before marking occupied; Thread 2: isAvailable() -> true, both proceed to park, causing double-booking.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
Hint: Check availability -> context switch -> both park -> double-booking [check]
Common Mistakes:
4. You want to add a
WeekendFeeStrategy to calculate parking fees differently on weekends using the current Template Method design. What is the minimum change that respects SOLID principles?medium
Solution
Step 1: Identify applicable SOLID principle
Open/Closed Principle (OCP) requires extending behavior without modifying existing code.Step 2: Use strategy injection
Creating a newFeeStrategyclass for weekends and injecting it respects OCP and Dependency Inversion Principle (DIP).Final Answer:
Option C -> Option CQuick Check:
New strategy class added; no existing class modified [check]
Hint: OCP: add new class, don't modify existing [check]
Common Mistakes:
5. In a parking lot system, if
parkVehicle() is not synchronized, which exact sequence of Thread 1 and Thread 2 causes double-booking of the same spot?medium
Solution
Step 1: Identify the 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 it as available.Step 2: Both threads proceed to park, causing double-booking
Because the state update is not atomic, both threads park vehicles in the same spot simultaneously.Final Answer:
Option C -> Option CQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
Hint: Check-then-act gap causes double-booking without synchronization [check]
Common Mistakes:
