Practice
1. A logging framework has a
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?easy
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]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
2. In designing a
Vehicle abstraction, should it be an abstract class or an interface if it needs to hold common state like speed and fuelLevel? Why?easy
Solution
Step 1: Analyze state and behavior requirements
SinceVehicleneeds to hold common state likespeedandfuelLevel, it requires instance variables.Step 2: Choose abstraction supporting state
Abstract classes can have instance variables and shared method implementations, whereas interfaces cannot hold instance state (only constants and default methods without state).Final Answer:
Option B -> Option BQuick Check:
State requires instance variables -> abstract class [check]
Hint: State needs fields -> abstract class; interfaces only declare behavior [check]
Common Mistakes:
3. In a multi-threaded parking lot system, if
parkVehicle() is not synchronized, which exact sequence of Thread 1 and Thread 2 calls causes double-booking of the same spot?medium
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]
Hint: Check availability then park must be atomic to avoid double-booking [check]
Common Mistakes:
4. To support
ElectricCar needing ChargingSpot in the file system's parking analogy, what is the minimum change to the current design to maintain extensibility without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements without modifying existing classes
Adding new vehicle and spot types should not require changing existing class code to avoid violating OCP.Step 2: Use enums for types and external compatibility map to add new types and compatibility rules
This allows adding ElectricCar and ChargingSpot by extending enums and maps without modifying existing classes.Step 3: Create ElectricCar subclass for specific behavior without altering Vehicle base class
This maintains clean separation and extensibility.Final Answer:
Option A -> Option AQuick Check:
Extend enums + external map + subclass -> minimal change [check]
Hint: Extend enums and maps, subclass new type, no existing class edits [check]
Common Mistakes:
5. To add
ElectricCar and ChargingSpot support without modifying existing classes, what is the minimum change to the current Java design?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclasses allows extension without changing existing class code, respecting OCP.Step 2: Use compatibility map update for new spot-vehicle matching
Updating SpotCompatibility.COMPATIBILITY_MAP with new types supports new logic without modifying existing classes' internals.Final Answer:
Option D -> Option DQuick Check:
New enums + subclasses + compatibility map update -> minimal change, no existing class modification [check]
Hint: Add enums + subclasses + compatibility map -> extend without modify [check]
Common Mistakes:
