Practice
FeeCalculator class. To add a new WeekendFeeStrategy without modifying existing code, which SOLID principle applies and what is the minimum change required?Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: How to apply OCP here
Create a new class WeekendFeeStrategy implementing FeeStrategy interface; FeeCalculator uses polymorphism to apply different strategies without modification.Final Answer:
Option A -> Option AQuick Check:
New strategy class + polymorphism -> OCP compliance [check]
parkVehicle() method checks if a spot is available and then marks it occupied. Without synchronization, which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Trace parkVehicle() check-then-act sequence
Thread 1 reads spot as available (true), then context switch occurs before marking occupied.Step 2: Thread 2 also reads spot as available (true) and proceeds to mark it occupied, causing double-booking.
Final Answer:
Option B -> Option BQuick Check:
Check-then-act race without synchronization -> double-booking [check]
parkVehicle() method checks if a spot is available and then marks it occupied. Without synchronized, what exact thread interleaving causes double-booking?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available but before marking occupied, context switches to Thread 2 which also reads available.Step 2: Both threads proceed to mark the spot occupied independently
This leads to double-booking as both think the spot was free at check time.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap -> both threads see available -> double-booking [check]
parkVehicle() is not synchronized, which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available and is preempted before marking it occupied. Thread 2 also reads available and proceeds to park.Step 2: Confirm both threads park concurrently causing double-booking
Both threads believe the spot is free and park, leading to inconsistent state and double-booking.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double-booking race [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]
