Practice
Vehicle be an abstract class or an interface, and why?Solution
Step 1: Analyze Vehicle's shared state and behavior
Vehicle has common fields (e.g., licensePlate) and methods (e.g., startEngine) that require shared implementation and state storage.Step 2: Abstract class vs interface suitability
Abstract classes allow shared state and method implementations, while interfaces cannot hold instance state. Thus, abstract class fits Vehicle better.Final Answer:
Option A -> Option AQuick Check:
Shared state + behavior -> abstract class [check]
scheduleJob() method is not synchronized, which exact thread interleaving causes the same Job to be scheduled twice?Solution
Step 1: Identify the check-then-act race condition
Both threads check if the job is already scheduled before adding. Without synchronization, both see job absent.Step 2: Trace thread interleaving
Thread 1 checks -> false, context switch before adding; Thread 2 checks -> false, adds job; Thread 1 resumes and adds job again, causing double scheduling.Final Answer:
Option C -> Option CQuick Check:
Check-then-act gap without synchronization -> double scheduling [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new classes for ElectricCar and ChargingSpot allows extension without changing existing class code, preserving OCP.Step 2: Why updating compatibility map is needed
SpotCompatibility.COMPATIBILITY_MAP controls which vehicles fit which spots; updating it ensures ElectricCar and ChargingSpot compatibility without modifying base classes.Final Answer:
Option B -> Option BQuick Check:
New enums + classes + compatibility map update -> minimal existing class changes [check]
ElectricCar needing ChargingSpot in the file system's parking analogy, what is the minimum change to the current design to maintain extensibility without modifying existing classes?Solution
Step 1: Identify extensibility requirements without modifying existing classes
Adding new vehicle and spot types should not require changing existing class code to avoid violating OCP.Step 2: Use enums for types and external compatibility map to add new types and compatibility rules
This allows adding ElectricCar and ChargingSpot by extending enums and maps without modifying existing classes.Step 3: Create ElectricCar subclass for specific behavior without altering Vehicle base class
This maintains clean separation and extensibility.Final Answer:
Option A -> Option AQuick Check:
Extend enums + external map + subclass -> minimal change [check]
ElectricCar and ChargingSpot in an existing parking system using Observer pattern. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not modify existing classes to respect OCP.Step 2: Evaluate options
Modify Vehicle and Spot classes to include electric-specific fields and logic for charging compatibility. and D modify existing classes, violating OCP; Create a new ElectricCar class and ChargingSpot class, then modify the Subject to notify these new types explicitly. modifies Subject, increasing coupling.Step 3: Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. adds new enum values and subclasses, updating compatibility externally, preserving existing class integrity.
Final Answer:
Option D -> Option DQuick Check:
New enums and subclasses added externally -> existing classes unchanged [check]
