Practice
1. In the hotel system, should
Vehicle be designed as an abstract class or an interface, considering that different vehicle types have state like license plate and behavior like parking? Why?easy
Solution
Step 1: Analyze Vehicle's state and behavior
Vehicle has common state (license plate) and behavior (park, unpark) shared by all types.Step 2: Abstract class vs interface for state and behavior
Abstract classes can hold state and provide default behavior; interfaces cannot hold instance state (Java 8+ default methods don't hold state). Thus, abstract class fits better.Final Answer:
Option B -> Option BQuick Check:
Shared state + behavior -> abstract class [check]
Hint: Stateful base class -> abstract class, not interface [check]
Common Mistakes:
2. In the Food Ordering System, the
KitchenQueue class has a parkOrder() method that checks if a slot is available before assigning an order. Without synchronization on parkOrder(), which exact thread interleaving causes double-booking of the same slot?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads slot as available but is preempted before assignment; Thread 2 also reads available and proceeds.Step 2: Both threads assign the same slot leading to double-booking
Because the availability check and assignment are not atomic, both threads assign the same slot concurrently.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double assignment [check]
Hint: Check availability -> context switch -> both assign same slot [check]
Common Mistakes:
3. In a plugin loader, the
loadPlugin() method checks if a plugin is already loaded before loading it. Without synchronizing this method, what exact thread interleaving causes the same plugin to be loaded twice?medium
Solution
Step 1: Trace the check-then-act sequence without synchronization
Thread 1 calls isLoaded() and sees false, then context switches before loading.Step 2: Thread 2 also calls isLoaded() and sees false, both threads proceed to load plugin concurrently, causing duplicate loading.
Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act allows double loading due to race [check]
Hint: Check-then-act without sync -> double load race condition [check]
Common Mistakes:
4. In the in-memory file system, the
createFile() method checks if a filename is available and then adds the file to the directory without synchronization. Which exact thread interleaving causes two threads to create files with the same name (double creation)?medium
Solution
Step 1: Trace the check-then-act sequence without synchronization
Thread 1 calls isNameAvailable() -> true, but before adding the file, context switches to Thread 2.Step 2: Thread 2 also calls isNameAvailable() -> true and proceeds to add the file, then Thread 1 resumes and adds its file too.
This causes double creation due to the race condition on the availability check and add operation.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without sync -> double creation [check]
Hint: Check availability -> context switch -> both add same file [check]
Common Mistakes:
5. You want to add a WeekendFeeStrategy to an ATM system using the State pattern for fee calculation. Which change respects the Open-Closed Principle (OCP)?
medium
Solution
Step 1: Understand OCP requirements
OCP requires extending behavior without modifying existing code.Step 2: Evaluate options
Add a boolean isWeekend flag to the existing FeeStrategy interface and handle it internally adds a flag to interface and implements logic in existing classes, avoiding new classes but modifying interface (violates OCP). Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it where needed adds a new class implementing FeeStrategy, extending behavior without modifying existing code, respecting OCP.Final Answer:
Option A -> Option AQuick Check:
New class for new fee strategy -> OCP respected [check]
Hint: OCP: extend with new class, don't modify existing code [check]
Common Mistakes:
