Practice
Table class has a private constructor and a static synchronized getInstance() method returning the single instance. Which pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and static method
A private constructor prevents external instantiation, and a static synchronizedgetInstance()method ensures controlled access to a single instance.Step 2: Match pattern intent to scenario
The Singleton pattern guarantees a single instance per JVM, suitable for a Table class representing a unique in-memory table. Factory Method and Builder focus on object creation but don't enforce single instance. Prototype clones objects, which is unrelated here.Final Answer:
Option D -> Option DQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
parkVehicle() method checks if a parking spot is available and then marks it occupied. Without synchronization, which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as free (isAvailable() -> true) but is preempted before marking it occupied.Step 2: Thread 2 also reads spot as free and proceeds to mark it occupied, causing double-booking.
This is the classic check-then-act gap causing race conditions.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
assignLocker() method is not synchronized, which exact sequence of thread interleaving causes two packages to be assigned the same locker?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads locker as available, then context switches before marking it occupied.Step 2: Thread 2 also reads locker as available and proceeds to assign it, causing double-booking.
Both threads assign the same locker before either updates the status.Final Answer:
Option C -> Option CQuick Check:
Check-then-act gap -> double assignment [check]
ElectricCar and ChargingSpot to the existing Abstract Factory design without modifying existing classes. What is the minimum change to support this extensibility in Java?Solution
Step 1: Identify extensibility requirements
New vehicle and spot types must be added without modifying existing classes to respect OCP.Step 2: Apply Java-specific extensibility
Adding new enum values and creating new classes for ElectricCar and ChargingSpot allows extension without modifying existing classes. Updating compatibility map centralizes compatibility logic.Step 3: Eliminate violating options
Options B and D modify existing classes, violating OCP. Create a new factory subclass for electric vehicles and modify the client code to use it. requires client code changes but not existing classes.Final Answer:
Option A -> Option AQuick Check:
New enums and classes added, existing classes untouched [check]
ElectricCar needing ChargingSpot support in the current parking lot design, what is the minimum change that respects extensibility and avoids modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not modify existing classes to respect Open-Closed Principle and avoid class explosion.Step 2: Evaluate minimal change approach
Extending enums and compatibility map, plus subclassing ElectricCar, allows new types without changing existing classes, preserving extensibility.Final Answer:
Option A -> Option AQuick Check:
Extending enums + subclass + compatibility map update -> minimal change [check]
