Practice
Playlist class has a private constructor and a static synchronized getInstance() method that returns the single instance. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the pattern from private constructor and static synchronized getInstance()
This is the Singleton pattern, which restricts instantiation to one object and provides a global access point.Step 2: Why Singleton fits Playlist playback queue management
Since the playback queue must be globally consistent and unique per user session, Singleton ensures only one instance manages it, avoiding conflicting states.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
WeekendFeeStrategy to calculate parking fees differently on weekends. Given the current design uses a FeeStrategy interface, what is the minimum change to comply with SOLID principles?Solution
Step 1: Identify SOLID principle
Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Apply OCP
Creating a newWeekendFeeStrategyclass extends functionality without modifying existing code, adhering to OCP and Dependency Inversion Principle.Final Answer:
Option C -> Option CQuick Check:
New strategy class -> extend without modifying existing -> OCP [check]
WeekendFeeStrategy to calculate weekend parking fees requires which minimum change to the current design that uses a FeeStrategy interface?Solution
Step 1: Understand current design with FeeStrategy interface
FeeStrategy interface allows different fee calculation implementations, supporting OCP if new strategies are added as new classes.Step 2: Analyze minimal change for weekend fees
Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without modifying existing classes respects OCP by adding a new class without modifying existing ones. Add a boolean flagisWeekendinParkingLotand conditionally apply fees inside existing strategies modifies ParkingLot to add weekend logic, violating OCP but minimal change.Final Answer:
Option D -> Option DQuick Check:
Minimal change but violates OCP -> modify ParkingLot with weekend flag [check]
ChargingSpot compatibility in the Jigsaw Puzzle System. What is the minimum design change to support ElectricCar and ChargingSpot without modifying existing classes?Solution
Step 1: Identify extensibility requirements
New piece and spot types must be added without modifying existing classes to respect OCP.Step 2: Evaluate minimal change approach
Adding new enum values and subclass for ElectricCarPiece plus updating compatibility map externally allows extension without modifying existing code.Final Answer:
Option A -> Option AQuick Check:
New enums and subclass extend system without changing existing classes [check]
ElectricCar vehicles requiring ChargingSpot types. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
New vehicle and spot types must be added without modifying existing classes to respect OCP.Step 2: Apply minimal change
Add new enum constants ELECTRIC_CAR and CHARGING. Create ElectricCar subclass. Update compatibility map externally to include new types. No existing classes modified.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + external map update -> no existing class changes [check]
