Practice
1. A class has a private constructor and a static synchronized
getInstance() method returning a single instance. Which pattern is applied and why is this the CORRECT use case?easy
Solution
Step 1: Identify the pattern by constructor and method signature
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method returns a single shared instance, matching Singleton semantics.Step 2: Explain why Singleton is correct here
Singleton ensures a single instance with thread-safe lazy initialization via synchronization, which fits the described code. Factory Method and Builder patterns do not restrict instance count, and Prototype involves cloning, not single instance control.Final Answer:
Option B -> Option BQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Hint: Private constructor + static synchronized getInstance() -> Singleton [check]
Common Mistakes:
2. A class has a private constructor and a static synchronized
getInstance() method returning the single instance. Which pattern is applied and why is this the CORRECT use case?easy
Solution
Step 1: Identify Singleton pattern characteristics
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance with thread-safe lazy initialization.Step 2: Eliminate other creational patterns
Factory Method controls subclass instantiation, Builder separates construction from representation, and Prototype clones objects; none enforce single instance or thread safety.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Hint: Private constructor + synchronized getInstance() -> Singleton [check]
Common Mistakes:
3. In a parking system,
parkVehicle() checks if a spot is available and then marks it occupied. Without synchronized, which exact thread interleaving causes double-booking?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available but is preempted before marking it occupied. Thread 2 also reads available and proceeds to mark occupied.Step 2: Confirm double-booking scenario
Both threads believe the spot is free and mark it occupied, causing double-booking due to the race between check and set.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
Hint: Check-then-act without lock -> race condition double-booking [check]
Common Mistakes:
4. To support
ElectricCar jobs requiring ChargingSpot resources in the Job Scheduler, what is the minimum change to the current design to maintain extensibility without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
ElectricCar and ChargingSpot are new types requiring minimal impact on existing Vehicle and Spot classes.Step 2: Apply extensibility without modifying existing classes
Add new enum values for types, create ElectricCar subclass, and update SpotCompatibility map externally to maintain OCP and avoid class explosion.Final Answer:
Option B -> Option BQuick Check:
New enums + subclass + external compatibility update -> minimal existing code change [check]
Hint: Add new enums + subclass + external map update -> minimal change [check]
Common Mistakes:
5. ElectricCar needs to be added to the product catalog with ChargingSpots. What is the minimum change to support this without modifying existing classes?
hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new classes for ElectricCar and ChargingSpot allows extension without changing existing class code, preserving OCP.Step 2: Why updating compatibility map is needed
SpotCompatibility.COMPATIBILITY_MAP controls which vehicles fit which spots; updating it ensures ElectricCar and ChargingSpot compatibility without modifying base classes.Final Answer:
Option B -> Option BQuick Check:
New enums + classes + compatibility map update -> minimal existing class changes [check]
Hint: Add enums + classes + map, no base class changes [check]
Common Mistakes:
