Practice
Conversation class has a private constructor and a static synchronized getInstance() method that returns the single instance per conversation ID. Which design pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method
The private constructor and static synchronizedgetInstance()method indicate Singleton pattern usage, controlling instance creation.Step 2: Understand why Singleton fits this use case
Singleton ensures only one Conversation instance per conversation ID, preventing duplicate conversation objects and maintaining consistent state.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
PluginRegistry class that holds a private static instance and exposes a synchronized getInstance() method. Which design pattern is applied here, and why is this the correct choice for the registry?Solution
Step 1: Identify the pattern from private static instance and synchronized getInstance()
This is the Singleton pattern, which restricts instantiation to a single object and provides a global access point.Step 2: Explain why Singleton fits the PluginRegistry
PluginRegistry must be unique to avoid inconsistent plugin state or duplicate registrations; Singleton ensures this global uniqueness.Final Answer:
Option B -> Option BQuick Check:
Singleton ensures one global registry instance managing plugins [check]
loadPlugin() method checks if a plugin is already loaded before loading it. Without synchronizing this method, what exact thread interleaving causes the same plugin to be loaded twice?Solution
Step 1: Trace the check-then-act sequence without synchronization
Thread 1 calls isLoaded() and sees false, then context switches before loading.Step 2: Thread 2 also calls isLoaded() and sees false, both threads proceed to load plugin concurrently, causing duplicate loading.
Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act allows double loading due to race [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Enums VehicleType and SpotType can be extended with new constants without changing existing class logic. Creating ElectricCar subclass extends Vehicle cleanly.Step 2: Update SpotCompatibility map externally
Maintaining compatibility mapping outside existing classes avoids modifying them, preserving OCP and avoiding class explosion.Final Answer:
Option A -> Option AQuick Check:
Extend enums + subclass + external compatibility map -> minimal change [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and a subclass extends functionality without changing existing class code, respecting OCP.Step 2: Evaluate options
ModifyVehicleclass to include achargingRequiredflag and update all spot checks. modifies Vehicle, violating OCP. Create a newChargingSpotclass and modifyParkingLotto handle it specially. requires modifying ParkingLot, violating minimal change. Add a new interfaceChargeableand makeElectricCarimplement it, changing existing spot logic. adds interface but requires changing spot logic, violating minimal change.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + compatibility map update -> minimal change [check]
