Practice
Vehicle abstraction for an adapter pattern implementation. Should Vehicle be an abstract class or an interface, and why?Solution
Step 1: Understand Vehicle abstraction needs
Vehicle defines behavior (methods) but should not hold state to allow flexible adapter implementations and multiple inheritance.Step 2: Evaluate Java constructs
Interfaces define behavior contracts without state, allowing multiple inheritance. Abstract classes can hold state but limit inheritance.Final Answer:
Option D -> Option DQuick Check:
Interface defines behavior only, enabling flexible adapters [check]
parkVehicle() method checks if a spot is available and then marks it occupied. Without synchronization, which exact thread interleaving causes double-booking?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available (true) but before marking it occupied, context switches.Step 2: Trace Thread 2's actions
Thread 2 also reads spot as available (true) and proceeds to mark it occupied. Both threads think the spot is free and park vehicles, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Check-then-act without sync allows both threads to park simultaneously [check]
makeMove() method, which exact sequence of thread interleaving causes two players to move simultaneously, violating turn order?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]
ElectricCar bidders requiring ChargingSpot reservations in the auction system, what is the minimum change to the current Java design to maintain extensibility without modifying existing classes?Solution
Step 1: Identify extensibility without modification
Adding new enum values and subclasses extends functionality without changing existing class code.Step 2: Update SpotCompatibility map externally to maintain compatibility rules without modifying existing classes.
Final Answer:
Option B -> Option BQuick Check:
Extend enums + subclass + external map update -> minimal change [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and a subclass extends functionality without changing existing class code, respecting OCP.Step 2: Evaluate options
ModifyVehicleclass to include achargingRequiredflag and update all spot checks. modifies Vehicle, violating OCP. Create a newChargingSpotclass and modifyParkingLotto handle it specially. requires modifying ParkingLot, violating minimal change. Add a new interfaceChargeableand makeElectricCarimplement it, changing existing spot logic. adds interface but requires changing spot logic, violating minimal change.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + compatibility map update -> minimal change [check]
