Practice
Vehicle be an abstract class or an interface, considering vehicles have state like speed and direction? Why?Solution
Step 1: Analyze state vs behavior requirements
Vehicles have shared state (speed, direction) and behavior; interfaces cannot hold instance state, so interface alone is insufficient.Step 2: Abstract class encapsulates shared state and common methods
Abstract class allows shared fields and partial implementation, enabling subclasses to extend and customize behavior.Final Answer:
Option C -> Option CQuick Check:
Stateful base class -> abstract class, not interface [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]
WeekendFeeStrategy for weekend bookings in the Airline Reservation System, which minimal change respects SOLID principles?Solution
Step 1: Identify SOLID principle involved
The Open-Closed Principle (OCP) requires that existing classes remain unchanged when adding new behavior.Step 2: Evaluate options for OCP compliance
Add a boolean flagisWeekendto theBookingclass and handle fees there. adds a flag and handles fees in Booking, violating SRP and mixing concerns. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without changing existing classes. cleanly adds a new strategy class without modifying existing code, respecting OCP and DIP.Final Answer:
Option B -> Option BQuick Check:
New FeeStrategy class -> no existing class modification -> OCP respected [check]
OrderBook class, the placeOrder() method checks if the order slot is available and then books it. Without synchronization on placeOrder(), which exact sequence of thread interleaving causes double-booking of the same slot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads slot as free but is preempted before booking; Thread 2 also reads free and proceeds to book.Step 2: Why this causes double-booking
Both threads believe the slot is free and book it concurrently, causing inconsistent state and double-booking.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without lock -> double-booking [check]
ElectricCar and ChargingSpot in the existing parking system using the command pattern. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding new vehicle and spot types without modifying existing classes enforces OCP and avoids class explosion.Step 2: Evaluate options for minimal invasive change
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar extends Vehicle, and update SpotCompatibility.COMPATIBILITY_MAP accordingly adds new enum values and new class, updating compatibility map without changing existing classes. Options B, C, D modify existing classes or add complex logic violating OCP.Final Answer:
Option C -> Option CQuick Check:
New enums and class added, existing classes unchanged [check]
