Practice
1. In designing the Wallet System, should the
Account class be an abstract class or an interface, considering it holds state like balance and transaction history? Why?easy
Solution
Step 1: Analyze state and behavior requirements
The Account class holds mutable state (balance, transaction history) and common behavior (debit, credit), which requires shared implementation and fields.Step 2: Abstract class vs interface for stateful behavior
Interfaces cannot hold instance state (fields), only constants and default methods without backing fields. Abstract classes allow fields and method implementations, making them suitable for shared state and behavior.Final Answer:
Option D -> Option DQuick Check:
Stateful class with shared behavior -> Abstract class [check]
Hint: State + behavior shared -> abstract class, not interface [check]
Common Mistakes:
2. In the Elevator System, the
assignElevatorToFloorRequest() method checks if an elevator is available before assigning it. Without synchronizing this method, what exact sequence of thread interleaving can cause two threads to assign the same elevator simultaneously?medium
Solution
Step 1: Identify check-then-act race condition
Without synchronization, Thread 1 reads elevator as available, then context switches before assignment.Step 2: Thread 2 also reads elevator as available and proceeds to assign it, causing double-booking.
Both threads assign the same elevator because the availability check and assignment are not atomic.Final Answer:
Option C -> Option CQuick Check:
Unsynchronized check-then-act causes double assignment [check]
Hint: Check-then-act without sync allows double assignment race [check]
Common Mistakes:
3. In a multi-threaded Java Observer implementation, if
notifyObservers() is not synchronized, which exact sequence of events can cause observers to receive inconsistent or duplicate updates?medium
Solution
Step 1: Identify race condition in notifyObservers()
Without synchronization, Thread 1 reads observer list size as 3, then context switches.Step 2: Thread 2 modifies observer list by removing one observer, reducing size to 2.
Thread 1 resumes and tries to notify 3 observers, causing out-of-date or duplicate notifications.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized notifyObservers() -> stale observer list read -> inconsistent notifications [check]
Hint: Unsynchronized notify reads stale observer list -> duplicate notifications [check]
Common Mistakes:
4. In a vending machine using the State pattern, if the method
selectItem() is not synchronized, which exact thread interleaving causes dispensing the same item twice?medium
Solution
Step 1: Identify the check-then-act race
Without synchronization, both threads can read item availability as true before either decrements inventory.Step 2: Trace the interleaving
Thread 1 checks availability -> true, context switch; Thread 2 checks availability -> true; both proceed to dispense, causing double dispense.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double dispense [check]
Hint: Check-then-act without sync -> double dispense race [check]
Common Mistakes:
5. 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:
