Practice
1. In a vehicle rental system, should
Vehicle be designed as an abstract class or an interface if it needs to hold common state like licensePlate and rentalStatus? Why?easy
Solution
Step 1: Analyze state requirements
Vehicle needs to hold common mutable state likelicensePlateandrentalStatus, which interfaces cannot hold as fields.Step 2: Choose correct OOP construct
Abstract classes can have instance variables and default method implementations, making them suitable for shared state. Interfaces cannot hold instance state, only constants.Final Answer:
Option D -> Option DQuick Check:
Shared mutable state -> abstract class, not interface [check]
Hint: Shared state needs abstract class, interfaces only define behavior [check]
Common Mistakes:
2. 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:
3. In the Online Blackjack game design, the
Deck class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct choice?easy
Solution
Step 1: Identify the pattern from constructor and method
A private constructor prevents external instantiation, and a static synchronizedgetInstance()ensures thread-safe single instance creation, matching Singleton pattern characteristics.Step 2: Why Singleton fits Deck
Deck must be unique to maintain consistent card state and prevent duplication, so Singleton enforces a single shared deck.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized static method -> Singleton pattern [check]
Hint: Private constructor + static synchronized method -> Singleton [check]
Common Mistakes:
4. In a multi-threaded logging framework, the
Appender class has a writeLog() method that first checks if the log file is open, then writes the log. Without synchronization on writeLog(), which exact thread interleaving causes lost log entries?medium
Solution
Step 1: Identify check-then-act race condition
Without synchronization, Thread 1 checks file open and sees true, then context switches before writing. Thread 2 also checks and sees true, both proceed to write concurrently.Step 2: Why this causes lost logs
Concurrent writes without synchronization cause race conditions, potentially overwriting or corrupting log entries.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap -> concurrent writes -> lost logs [check]
Hint: Check file open -> context switch -> both write -> race [check]
Common Mistakes:
5. To support
ElectricCar needing ChargingSpot in the vending machine parking design, what is the minimum change that respects extensibility without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
Adding new vehicle and spot types without modifying existing classes preserves OCP and avoids class explosion.Step 2: Evaluate options for minimal invasive change
AddELECTRIC_CARtoVehicleTypeenum,CHARGINGtoSpotTypeenum, createElectricCarsubclass, and updateSpotCompatibility.COMPATIBILITY_MAPaccordingly. adds new enum values and a subclass, updating compatibility map without changing existing classes. Others modify existing classes or add flags causing complexity.Final Answer:
Option B -> Option BQuick Check:
New enums and subclass extend design without modifying existing classes [check]
Hint: Add enums + subclass + compatibility map, no existing class changes [check]
Common Mistakes:
