Practice
Vehicle abstraction that requires both state (e.g., speed) and behavior (e.g., accelerate()), should Vehicle be an abstract class or an interface in Java, and why?Solution
Step 1: Analyze state and behavior requirements
Vehicleneeds to hold state like speed and provide behavior like accelerate(), which requires shared fields and possibly implemented methods.Step 2: Choose between abstract class and interface
Abstract classes can hold instance variables and provide method implementations, while interfaces (pre-Java 8) cannot hold state and only declare methods. Even with default methods, interfaces cannot hold mutable state.Final Answer:
Option D -> Option DQuick Check:
State + behavior -> abstract class fits better than interface [check]
Hand class has a method addCard(Card card) that is not synchronized. Two threads concurrently add cards to the same hand. Which exact sequence causes a race condition leading to lost cards?Solution
Step 1: Identify the check-then-act gap
Without synchronization, both threads read the current size of the card list before either adds their card.Step 2: Trace interleaving causing overwrite
Thread 1 reads size N, context switches; Thread 2 reads size N; both add at index N, causing one card to overwrite the other.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized addCard -> check-then-act race -> lost card [check]
parkVehicle() is NOT synchronized, which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Identify check-then-act race condition
Without synchronization, Thread 1 reads spot as available but before marking it occupied, context switches to Thread 2 which also reads available.Step 2: Both threads proceed to park, causing double-booking
Because the state update is not atomic, both threads believe the spot is free and park simultaneously.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap -> double-booking due to unsynchronized parkVehicle() [check]
ElectricCar and ChargingSpot in the existing in-memory SQL database's vehicle-parking module. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Enums can be extended with new constants without changing existing class logic. Creating new subclasses preserves existing class code.Step 2: Apply minimal change approach
Adding new enum values and new subclasses for ElectricCar and ChargingSpot allows extension without modifying existing classes. Updating compatibility map is necessary but localized.Final Answer:
Option A -> Option AQuick Check:
Extend enums + subclass + update compatibility map -> minimal change [check]
ElectricCar needing ChargingSpot support in the current parking lot design, what is the minimum change that respects extensibility and avoids modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not modify existing classes to respect Open-Closed Principle and avoid class explosion.Step 2: Evaluate minimal change approach
Extending enums and compatibility map, plus subclassing ElectricCar, allows new types without changing existing classes, preserving extensibility.Final Answer:
Option A -> Option AQuick Check:
Extending enums + subclass + compatibility map update -> minimal change [check]
