Practice
CarBuilder constructs a Car object step-by-step using methods like setEngine(), setWheels(), and finally build(). The Car constructor is private. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the construction process
The class uses multiple setter methods to configure parts of the object before finalizing withbuild(), indicating stepwise construction.Step 2: Match pattern intent
Builder pattern separates construction from representation, ideal for complex objects with many optional parts. Factory Method uses inheritance, Prototype clones objects, Singleton restricts instance count.Final Answer:
Option A -> Option AQuick Check:
Stepwise object creation with private constructor -> Builder pattern [check]
getInstance() method that returns the 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 instance is created lazily and safely in multithreaded contexts.Step 2: Eliminate other creational patterns
Factory Method uses subclassing, Builder separates construction steps, and Prototype clones objects; none match the private constructor plus static synchronized method pattern.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
parkVehicle() is not synchronized, which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Identify race condition in parkVehicle()
Without synchronization, two threads can both check spot availability before either marks it occupied.Step 2: Trace exact interleaving
Thread 1 calls isAvailable() -> true, context switch before marking occupied; Thread 2 calls isAvailable() -> true; both proceed to park, causing double-booking.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without synchronization causes double-booking [check]
parkVehicle() is not synchronized, which exact sequence of Thread 1 and Thread 2 calls causes double-booking of the same spot?Solution
Step 1: Identify race condition
Without synchronization, both threads can read spot availability as true before either marks it occupied.Step 2: Trace exact interleaving
Thread 1: isAvailable() -> true, context switch; Thread 2: isAvailable() -> true; both proceed to park, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
ElectricCar and ChargingSpot without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses allows extension without changing existing class code.Step 2: Update compatibility map externally to maintain open-closed principle and avoid class explosion.
Options B, C, and D require modifying existing classes, violating OCP or causing tight coupling.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + external compatibility update -> minimal change [check]
