Practice
1. A
ParkingLot class has a private constructor and a static synchronized getInstance() method returning the single instance. Which design pattern is applied here and why is this the correct use case?easy
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 and accessed globally.Step 2: Why Singleton fits ParkingLot
ParkingLot must be unique to coordinate spot allocation and pricing consistently across the system, so Singleton enforces this single shared instance.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Hint: Private constructor + static getInstance() = Singleton [check]
Common Mistakes:
2. In the Restaurant Management System, the
OrderManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance, which matches the Singleton pattern.Step 2: Why Singleton fits OrderManager
OrderManager must coordinate all orders centrally to avoid conflicting states; multiple instances would cause inconsistent order tracking.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized static getInstance() -> Singleton [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
3. In the shopping cart system, should
Discount be designed as an abstract class or an interface? Consider that discounts have varying calculation logic but share no common state.easy
Solution
Step 1: Analyze shared state vs behavior
Discounts vary in calculation logic and do not share common fields, so no shared state exists.Step 2: Choose interface for behavior without state
Interfaces define behavior contracts without enforcing state, fitting varying discount calculations better than abstract classes.Final Answer:
Option D -> Option DQuick Check:
No shared state + varying behavior -> interface [check]
Hint: No shared state + varying logic -> interface [check]
Common Mistakes:
4. A class has a private constructor and a static synchronized
getInstance() method that returns the single instance. Which design pattern is applied and why is this the CORRECT use case?easy
Solution
Step 1: Identify the pattern from private constructor and static synchronized 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 controls creation but allows multiple instances; Builder separates construction steps; Prototype clones instances. None enforce single instance with thread safety.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> thread-safe Singleton [check]
Hint: Private constructor + synchronized getInstance() -> Singleton [check]
Common Mistakes:
5. In the Hotel Management System, if the
parkVehicle() method is not synchronized, which exact sequence of events between Thread 1 and Thread 2 causes double-booking of the same parking spot?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available and is preempted before marking it occupied.Step 2: Thread 2 also reads spot as available and proceeds to park, causing double-booking.
This is the classic check-then-act gap causing race condition.Final Answer:
Option A -> Option AQuick Check:
Check availability -> true by both threads before marking occupied -> double-booking [check]
Hint: Check-then-act gap -> double-booking without synchronization [check]
Common Mistakes:
