Practice
1. In a Chess game design, the
Board 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 role of private constructor and static synchronized method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()ensures thread-safe single instance creation.Step 2: Match to design pattern intent
This matches the Singleton pattern, which restricts instantiation to one object, suitable for a unique game board shared across the system.Final Answer:
Option A -> Option AQuick Check:
Singleton ensures single board instance across threads [check]
Hint: Private constructor + static synchronized method -> Singleton [check]
Common Mistakes:
2. In designing the Meeting Scheduler, should the
Attendee entity be modeled as an abstract class or an interface, considering that attendees can be both internal employees and external guests with different data and behaviors?easy
Solution
Step 1: Analyze state and behavior needs
Attendee has shared state (name, email) and some common behavior, which favors abstract class to avoid duplicating fields.Step 2: Evaluate interface limitations
Interfaces cannot hold state, so using interface would force duplication or external state holders, complicating design.Final Answer:
Option C -> Option CQuick Check:
Shared state + behavior -> abstract class [check]
Hint: Shared state? Use abstract class, not interface [check]
Common Mistakes:
3. 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:
4. In a traffic light system design, a
TrafficLightController 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()ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Why Singleton fits traffic light controller
Only one controller should coordinate the traffic lights to avoid conflicting states; multiple instances would cause inconsistent signals.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + static synchronized getInstance() -> Singleton [check]
Common Mistakes:
5. 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? Why?easy
Solution
Step 1: Identify state holding needs
licensePlateis shared state, which interfaces cannot hold as fields.Step 2: Evaluate options
Abstract classes can hold state and provide default behavior; interfaces cannot hold instance fields, only constants.Final Answer:
Option D -> Option DQuick Check:
Abstract class holds shared state like licensePlate [check]
Hint: State needs abstract class, interfaces only declare behavior [check]
Common Mistakes:
