Practice
1. In the Hotel Management System, the
RoomManager 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
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 RoomManager
RoomManager manages shared state like room availability; having multiple instances risks inconsistent state. Singleton ensures centralized control.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized static getInstance() -> Singleton [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
2. In designing a
Vehicle abstraction for a parking system, should Vehicle be an abstract class or an interface if it needs to hold state like licensePlate and provide default behavior for getSize()? Why?easy
Solution
Step 1: Analyze state and behavior requirements
Vehicle needs to hold state (licensePlate) and provide default behavior (getSize), which interfaces cannot hold as fields.Step 2: Choose abstraction supporting state and default behavior
Abstract classes support instance variables and method implementations; interfaces cannot hold instance state.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports state and default behavior -> correct choice [check]
Hint: State needs abstract class; interfaces can't hold instance fields [check]
Common Mistakes:
3. A class provides a
createIterator() method that returns an object allowing traversal over its elements without exposing its internal data structure. Which design pattern is being applied and why is this the correct choice?easy
Solution
Step 1: Identify the role of
This method returns an object that allows clients to traverse elements without exposing the collection's internal representation, which is the core intent of the Iterator pattern.createIterator()Step 2: Eliminate other patterns
Factory Method focuses on object creation, Decorator adds responsibilities dynamically, and Composite manages tree structures. None focus on traversal abstraction.Final Answer:
Option C -> Option CQuick Check:
Iterator separates traversal from collection internals [check]
Hint: Iterator hides structure, exposes traversal [check]
Common Mistakes:
4. In a multi-threaded Java Observer implementation, if
notifyObservers() is not synchronized, which exact sequence of events can cause observers to receive inconsistent or duplicate updates?medium
Solution
Step 1: Identify race condition in notifyObservers()
Without synchronization, Thread 1 reads observer list size as 3, then context switches.Step 2: Thread 2 modifies observer list by removing one observer, reducing size to 2.
Thread 1 resumes and tries to notify 3 observers, causing out-of-date or duplicate notifications.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized notifyObservers() -> stale observer list read -> inconsistent notifications [check]
Hint: Unsynchronized notify reads stale observer list -> duplicate notifications [check]
Common Mistakes:
5. To support
ElectricCar bidders requiring ChargingSpot reservations in the auction system, what is the minimum change to the current Java design to maintain extensibility without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modification
Adding new enum values and subclasses extends functionality without changing existing class code.Step 2: Update SpotCompatibility map externally to maintain compatibility rules without modifying existing classes.
Final Answer:
Option B -> Option BQuick Check:
Extend enums + subclass + external map update -> minimal change [check]
Hint: Extend enums + subclass + external map update -> minimal change [check]
Common Mistakes:
