Practice
1. In designing the vending machine's product selection module, should
Product be modeled as an abstract class or an interface? Consider that products have shared state like price and inventory count, but different behaviors like dispense() and getDescription().easy
Solution
Step 1: Analyze shared state and behavior
Products share common state like price and inventory count, which requires fields and possibly default implementations.Step 2: Choose between interface and abstract class
Abstract classes can hold state and provide default method implementations, while interfaces cannot hold instance state (only constants).Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and behavior [check]
Hint: Abstract class holds state; interface only behavior signatures [check]
Common Mistakes:
2. In designing a
Vehicle abstraction, should it be an abstract class or an interface if it needs to hold common state like speed and fuelLevel? Why?easy
Solution
Step 1: Identify need for shared state
SincespeedandfuelLevelare common state variables, the abstraction must hold state.Step 2: Choose between abstract class and interface
Interfaces cannot hold instance state (prior to Java 8 default methods, and even then no instance fields). Abstract classes can hold state and provide common behavior implementations.Final Answer:
Option A -> Option AQuick Check:
Shared state -> abstract class, not interface [check]
Hint: State needs abstract class; interfaces can't hold instance variables [check]
Common Mistakes:
3. In a multi-threaded Chess game server, without synchronizing the
makeMove() method, which exact sequence of thread interleaving causes two players to move simultaneously, violating turn order?medium
Solution
Step 1: Identify the check-then-act race condition
Both threads read the turn state before either updates it, causing both to believe it's their turn.Step 2: Trace thread interleaving
Thread 1 checks turn -> true, context switch before move; Thread 2 checks turn -> true, both move simultaneously, violating turn order.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap allows double move without synchronization [check]
Hint: Check turn then act without lock -> race condition [check]
Common Mistakes:
4. In the Airline Reservation System, if the
reserveSeat() method is not synchronized, which exact sequence of thread interleaving causes double-booking of the same seat?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads seat as available but is preempted before booking; Thread 2 also reads available and proceeds.Step 2: Both threads book the same seat concurrently
Because no lock or atomic operation protects the check-and-book sequence, double-booking occurs.Final Answer:
Option D -> Option DQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
Hint: Check seat available -> context switch -> both book [check]
Common Mistakes:
5. ElectricCar plugins require ChargingSpot support in the plugin architecture. What is the minimum change to support this without modifying existing classes?
hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclass ElectricCar extends Vehicle allows extension without changing existing code.Step 2: Update compatibility map to include new types without altering existing plugin or vehicle classes
This preserves OCP and avoids class explosion or god-class logic.Final Answer:
Option D -> Option DQuick Check:
Extend enums and subclass; update compatibility map; no existing class changes [check]
Hint: Extend enums + subclass + map update = minimal change [check]
Common Mistakes:
