Practice
Logger class with a private constructor and a static synchronized getInstance() method returning the single instance. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the pattern from private constructor and static getInstance()
The private constructor prevents external instantiation, and the static synchronized getInstance() ensures a single shared instance, which matches the Singleton pattern.Step 2: Why Singleton is correct here
Logging frameworks require a single global Logger instance to coordinate log output consistently. Factory, Builder, and Prototype patterns do not enforce single instance semantics.Final Answer:
Option D -> Option DQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
Elevator class implement an interface or extend an abstract class to represent different elevator types (e.g., passenger, freight)? Consider that elevators share common state and behavior but also have type-specific logic.Solution
Step 1: Analyze shared state and behavior
Elevators share common fields like currentFloor, direction, and doorStatus, plus some implemented methods.Step 2: Abstract class fits shared state and partial behavior
Abstract class allows code reuse and enforces common state, while subclasses override type-specific logic.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and behavior [check]
KitchenQueue class has a parkOrder() method that checks if a slot is available before assigning an order. Without synchronization on parkOrder(), which exact thread interleaving causes double-booking of the same slot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads slot as available but is preempted before assignment; Thread 2 also reads available and proceeds.Step 2: Both threads assign the same slot leading to double-booking
Because the availability check and assignment are not atomic, both threads assign the same slot concurrently.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double assignment [check]
parkVehicle() method is not synchronized. Which exact sequence of Thread 1 and Thread 2 calls causes double-booking of the same parking spot?Solution
Step 1: Identify check-then-act race condition
Without synchronization, Thread 1 reads spot as available but before marking it occupied, context switches to Thread 2 which also reads available.Step 2: Both threads proceed to park, causing double-booking
Both threads act on stale availability info, violating mutual exclusion.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
ElectricCar users who require ChargingSpot support in the Vehicle and ParkingSpot hierarchy. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses extends functionality without changing existing code.Step 2: Use external compatibility map update
Updating the compatibility map externally avoids modifying existing class logic, preserving OCP.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + external map update -> minimal change [check]
