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]
Condition component be an abstract class or an interface, and why?Solution
Step 1: Analyze Condition's role
Conditions define evaluation logic without requiring shared state; they represent a contract for boolean checks.Step 2: Interface vs Abstract class
Interface suits stateless behavior contracts and allows multiple inheritance; abstract class implies shared state, which is not needed and limits flexibility.Final Answer:
Option A -> Option AQuick Check:
Condition defines behavior only -> interface [check]
Vehicle abstraction, should it be an abstract class or an interface if it needs to hold common state like speed and fuelLevel? Why?Solution
Step 1: Analyze state and behavior requirements
SinceVehicleneeds to hold common state likespeedandfuelLevel, it requires instance variables.Step 2: Choose abstraction supporting state
Abstract classes can have instance variables and shared method implementations, whereas interfaces cannot hold instance state (only constants and default methods without state).Final Answer:
Option B -> Option BQuick Check:
State requires instance variables -> abstract class [check]
ElectricCar that requires ChargingSpot compatibility in the current parking system. What is the minimum change to the design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
ElectricCar and ChargingSpot are new types requiring compatibility mapping without changing existing classes.Step 2: Apply minimal invasive change
Adding new enum values and subclass plus updating compatibility map extends functionality without modifying existing class code, preserving OCP.Step 3: Avoid violating OCP
Options B, C, and D modify existing classes or create parallel hierarchies, increasing complexity and violating OCP.Final Answer:
Option B -> Option BQuick Check:
Extend enums and map, subclass ElectricCar -> minimal change [check]
Solution
Step 1: Identify extensibility without modifying existing classes
To comply with OCP and avoid class explosion, new types should be added via enums and new subclasses without changing existing code.Step 2: Explain why Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. fits minimal change
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. adds new enum values and creates a new ElectricCar subclass, updating compatibility externally, preserving existing classes unchanged. Options B, C, and D modify existing classes or create subclasses that duplicate logic, violating OCP or SRP.Final Answer:
Option D -> Option DQuick Check:
New enums + subclass + external compatibility update -> minimal change [check]
