Practice
Vehicle abstraction that requires both state (e.g., speed) and behavior (e.g., move()), should Vehicle be an abstract class or an interface? Why?Solution
Step 1: Analyze state and behavior requirements
Vehicle needs to hold state like speed and provide behavior like move(), which requires shared fields and method implementations.Step 2: Evaluate abstract class vs interface
Abstract classes can hold instance variables and provide default method implementations; interfaces cannot hold instance state (only constants) despite default methods.Final Answer:
Option A -> Option AQuick Check:
State + behavior -> abstract class, not interface [check]
Vehicle be modeled as an abstract class or an interface if it has state like speed and behavior like accelerate()? Why?Solution
Step 1: Analyze state and behavior requirements
Vehicle has state (speed) and behavior (accelerate()), so it needs a construct that supports instance variables and method implementations.Step 2: Evaluate abstract class vs interface
Abstract classes can hold state and provide default behavior; interfaces cannot hold instance state (only constants) and default methods can't maintain state.Final Answer:
Option B -> Option BQuick Check:
State + behavior with shared code -> abstract class [check]
RoomBookingManager class has a bookRoom() method that checks if a room is available and then marks it booked. Without synchronizing bookRoom(), which exact sequence of events between Thread 1 and Thread 2 causes a double-booking race condition?Solution
Step 1: Identify check-then-act gap
Without synchronization, Thread 1 reads availability as true but is preempted before booking; Thread 2 also reads true and proceeds.Step 2: Both threads book simultaneously causing double-booking
Because the availability check and booking are not atomic, both threads can book the same room concurrently.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
ElectricCar players who require ChargingSpot in the Tic-Tac-Toe game parking analogy, what is the minimum design change that respects extensibility without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not require modifying existing classes to avoid violating OCP and creating class explosion.Step 2: Why Add
Adding new enum values and a subclass keeps existing classes unchanged. Updating the compatibility map centralizes compatibility logic without scattering changes.ELECTRIC_CARtoVehicleTypeenum,CHARGINGtoSpotType, createElectricCarsubclass, and updateSpotCompatibility.COMPATIBILITY_MAPaccordingly. is minimal and extensibleStep 3: Why other options violate extensibility
Options B, C, and D modify existing classes or add conditional logic, increasing coupling and violating OCP.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + compatibility map update -> minimal, extensible [check]
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot must avoid modifying existing classes to respect OCP.Step 2: Evaluate options for minimal invasive change
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally adds new enum values and subclasses externally, updating compatibility map without changing existing code. Options B and C modify base classes, violating OCP. Create a god-class that handles both ElectricCar and ChargingSpot logic to avoid modifying existing classes creates god-class, violating SRP and maintainability.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + external map update -> minimal change, OCP respected [check]
