Practice
Track abstraction for the music library, should Track be an abstract class or an interface? Consider that each Track has state like duration and title, and behavior like play() and pause().Solution
Step 1: Analyze state and behavior requirements
Track has shared state (duration, title) and common behavior (play, pause) that can be implemented once.Step 2: Abstract class vs interface for state and behavior
Abstract class can hold state fields and provide default method implementations, while interfaces cannot hold instance state.Final Answer:
Option C -> Option CQuick Check:
State + default behavior -> abstract class [check]
Vehicle abstraction for a parking system, should Vehicle be an abstract class or an interface if it needs to hold state like licensePlate and provide default behavior for getSize()? Why?Solution
Step 1: Analyze state and behavior requirements
Vehicle needs to hold state (licensePlate) and provide default behavior (getSize), which interfaces cannot hold as fields.Step 2: Choose abstraction supporting state and default behavior
Abstract classes support instance variables and method implementations; interfaces cannot hold instance state.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports state and default behavior -> correct choice [check]
Solution
Step 1: Identify the role of state classes
Each state class implements the same interface but provides different behavior for the same method calls, which is polymorphism.Step 2: Eliminate other options
Inheritance alone does not explain different behaviors; encapsulation hides data but does not delegate behavior; abstraction defines interface but does not implement behavior.Final Answer:
Option D -> Option DQuick Check:
State classes override interface methods -> polymorphism [check]
DeliveryStatus class has a method markDelivered() that updates the delivery state. Without synchronizing this method, which exact thread interleaving causes inconsistent delivery status updates?Solution
Step 1: Identify the race condition in markDelivered()
Without synchronization, two threads can read the undelivered status before either writes the delivered status.Step 2: Trace the interleaving causing lost update
Thread 1 reads undelivered -> context switch -> Thread 2 reads undelivered -> both write delivered, one overwriting the other's update.Final Answer:
Option A -> Option AQuick Check:
Check-then-act without sync -> lost update race condition [check]
ElectricCar and ChargingSpot without modifying existing Vehicle or Spot classes. What is the minimum change to support this extensibility in Java?Solution
Step 1: Identify extensibility without modifying existing classes
Enums can be extended with new constants; subclassingElectricCaradds new behavior without changingVehicle.Step 2: Update compatibility externally
UpdatingSpotCompatibility.COMPATIBILITY_MAPexternally respects OCP and avoids class explosion or god classes.Final Answer:
Option B -> Option BQuick Check:
Extend enums + subclass + external map update -> minimal change [check]
