Practice
getInstance() method returning a single instance. Which pattern is applied and why is this the CORRECT use case?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance with thread safety.Step 2: Eliminate other creational patterns
Factory Method creates objects but doesn't enforce single instance; Builder constructs complex objects but not singleton; Prototype clones instances, not restricts to one.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized static getter -> Singleton pattern [check]
ConnectionManager, the method addConnection(User a, User b) checks if users are already connected before adding. Without synchronizing this method, what exact thread interleaving causes a double connection to be created?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads can see no existing connection and proceed to add, causing duplicates.Step 2: Trace exact thread interleaving
Thread 1 callsisConnected(a,b)and gets false, then context switches before adding. Thread 2 also callsisConnected(a,b)and gets false, then both add connections concurrently.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap causes double connection addition [check]
ElectricCar needing ChargingSpot in the vending machine parking design, what is the minimum change that respects extensibility without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding new vehicle and spot types without modifying existing classes preserves OCP and avoids class explosion.Step 2: Evaluate options for minimal invasive change
AddELECTRIC_CARtoVehicleTypeenum,CHARGINGtoSpotTypeenum, createElectricCarsubclass, and updateSpotCompatibility.COMPATIBILITY_MAPaccordingly. adds new enum values and a subclass, updating compatibility map without changing existing classes. Others modify existing classes or add flags causing complexity.Final Answer:
Option B -> Option BQuick Check:
New enums and subclass extend design without modifying existing classes [check]
ElectricCar and ChargingSpot in the existing parking system using the Iterator 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 require modifying existing classes to respect OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map; existing classes remain unchanged. adds new enums and classes without modifying existing ones, preserving OCP. Modify Vehicle and Spot classes to include electric-specific fields and logic, then update iterators accordingly. modifies existing classes, violating OCP. Create a new ElectricVehicle interface extending Vehicle and a ChargingSpot class, then modify existing iterators to handle them. adds interfaces but requires modifying iterators, violating OCP. Add a flag isElectric to Vehicle class and modify SpotCompatibility to check this flag during iteration. modifies Vehicle class, violating OCP.Final Answer:
Option A -> Option AQuick Check:
Adding new enums and subclasses without changing existing classes respects OCP [check]
ElectricCar needing ChargingSpot support, what is the minimum change to the current design without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot support must avoid modifying existing classes to comply with OCP and maintain code modularity.Step 2: Evaluate options for minimal invasive change
AddELECTRIC_CARtoVehicleType,CHARGINGtoSpotType, createElectricCarextendingVehicle, and updateSpotCompatibility.COMPATIBILITY_MAPaccordingly. extends enums and adds new classes, updating compatibility map without changing existing classes. Options B, C, and D modify existing classes or add god-class logic, violating OCP and increasing complexity.Step 3: Confirm Java-specific implementation details
Updating enums and compatibility map is idiomatic in Java for extensibility without class explosion.Final Answer:
Option A -> Option AQuick Check:
Extend enums + new class + compatibility map update -> minimal change [check]
