Practice
1. In a vehicle rental system, should
Vehicle be designed as an abstract class or an interface if it needs to hold common state like licensePlate and rentalStatus? Why?easy
Solution
Step 1: Analyze state requirements
Vehicle needs to hold common mutable state likelicensePlateandrentalStatus, which interfaces cannot hold as fields.Step 2: Choose correct OOP construct
Abstract classes can have instance variables and default method implementations, making them suitable for shared state. Interfaces cannot hold instance state, only constants.Final Answer:
Option D -> Option DQuick Check:
Shared mutable state -> abstract class, not interface [check]
Hint: Shared state needs abstract class, interfaces only define behavior [check]
Common Mistakes:
2. In designing the Parking Lot system, should
Vehicle be an abstract class or an interface, and why?easy
Solution
Step 1: Identify if Vehicle has shared state
Vehicle has common fields like license plate, size, and type that need to be stored and reused.Step 2: Abstract class vs interface for shared state
Interfaces cannot hold state in Java (prior to default methods), so abstract class is needed to encapsulate shared state and partial implementation.Final Answer:
Option A -> Option AQuick Check:
Shared state in Vehicle -> abstract class needed [check]
Hint: Shared state? Use abstract class, not interface [check]
Common Mistakes:
3. In designing the traffic light system, should
Vehicle be an abstract class or an interface, considering vehicles have state like speed and direction? Why?easy
Solution
Step 1: Analyze state vs behavior requirements
Vehicles have shared state (speed, direction) and behavior; interfaces cannot hold instance state, so interface alone is insufficient.Step 2: Abstract class encapsulates shared state and common methods
Abstract class allows shared fields and partial implementation, enabling subclasses to extend and customize behavior.Final Answer:
Option C -> Option CQuick Check:
Stateful base class -> abstract class, not interface [check]
Hint: Stateful base class -> abstract class, stateless -> interface [check]
Common Mistakes:
4. In a vending machine design, a
State interface defines methods like selectProduct(), insertMoney(), and dispense(). Concrete classes like IdleState, HasMoneyState, and SoldState implement this interface. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify the role of State interface and concrete states
TheStateinterface defines behavior that varies depending on the vending machine's current state. Concrete classes implement these behaviors and handle transitions.Step 2: Match pattern intent to scenario
The State pattern encapsulates state-specific behavior and transitions, allowing the vending machine to change behavior dynamically without complex conditionals.Final Answer:
Option D -> Option DQuick Check:
State pattern encapsulates state behavior and transitions [check]
Hint: State pattern encapsulates behavior per state [check]
Common Mistakes:
5. In a multi-threaded chain of responsibility, if the method
handleRequest() is not synchronized, which exact sequence of events can cause a request to be handled twice by two handlers?medium
Solution
Step 1: Identify race condition
Without synchronization, two threads can both see canHandle() as true before either marks the request handled.Step 2: Trace thread interleaving
Thread 1 checks canHandle() -> true, context switch before marking handled; Thread 2 also checks canHandle() -> true and proceeds; both handle the same request.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap causes double handling [check]
Hint: Check-then-act gap causes double handling [check]
Common Mistakes:
