Practice
1. In the Car Rental System, the
BookingManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here, and why is this the correct choice for managing bookings?easy
Solution
Step 1: Identify the pattern from constructor and method
A private constructor prevents external instantiation, and a static synchronizedgetInstance()method ensures a single shared instance, matching the Singleton pattern.Step 2: Reason why Singleton fits BookingManager
BookingManager must coordinate all bookings centrally to avoid conflicts; multiple instances would cause inconsistent state, so Singleton ensures global coordination.Final Answer:
Option A -> Option AQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
2. In the Facebook OOP design, the
User class has a private constructor and a static synchronized getInstance() method to ensure only one instance per user ID exists. 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 and static synchronizedgetInstance()method indicate Singleton usage to control instance creation.Step 2: Why Singleton fits here
Singleton ensures only one User instance per user ID exists, preventing duplicate objects and managing concurrency safely.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + synchronized getInstance() -> Singleton [check]
Common Mistakes:
3. In the Jigsaw Puzzle System, the
Board class has a placePiece() method that first checks if a position is free and then places the piece. Without synchronizing placePiece(), what exact sequence of thread interleaving causes two threads to place different pieces at the same position (double-booking)?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 checks position free and sees true, but before placing, context switches.Step 2: Thread 2 also checks position free -> true and proceeds to place piece concurrently, causing double-booking.
Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act allows both threads to place pieces at same position [check]
Hint: Check-then-act without sync -> double-booking race [check]
Common Mistakes:
4. The auction system currently calculates fees with a fixed strategy. To add a
WeekendFeeStrategy without modifying existing classes, which SOLID principle is being applied and what is the minimum required change?medium
Solution
Step 1: Identify SOLID principle
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP by adding WeekendFeeStrategy implementing FeeStrategy interface without modifying existing classes.
Final Answer:
Option A -> Option AQuick Check:
New strategy class + injection -> OCP compliance [check]
Hint: Add new strategy class, no existing code change -> OCP [check]
Common Mistakes:
5. You want to add a
WeekendFeeStrategy to the parking lot pricing system. Which minimum change respects SOLID principles?medium
Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires adding new functionality without modifying existing code.Step 2: Evaluate options
Modify the existingFeeCalculatorclass to add weekend logic inside. violates OCP by modifying existing class. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without changing existing classes. respects OCP by adding new strategy class. Add a boolean flagisWeekendtoVehicleand adjust pricing accordingly. incorrectly mixes state into Vehicle, violating SRP. Create a subclassWeekendFeeCalculatorextendingFeeCalculatorand override methods. adds subclass but may cause class explosion and tight coupling.Final Answer:
Option A -> Option AQuick Check:
New strategy class + no existing class change -> OCP respected [check]
Hint: Add new strategy class, avoid modifying existing code [check]
Common Mistakes:
