Practice
1. In a Java codebase, a factory class provides methods to create related objects like
Button and Checkbox families without specifying their concrete classes. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify the role of the factory class
The factory creates multiple related objects (Button, Checkbox) without exposing their concrete classes, indicating a family of products.Step 2: Match pattern intent
Abstract Factory pattern is designed to create families of related objects without specifying their concrete classes, unlike Builder (stepwise construction), Singleton (single instance), or Prototype (cloning).Final Answer:
Option C -> Option CQuick Check:
Abstract Factory creates related object families without concrete classes [check]
Hint: Abstract Factory creates related families, not single objects [check]
Common Mistakes:
2. In the hotel system, should
Vehicle be designed as an abstract class or an interface, considering that different vehicle types have state like license plate and behavior like parking? Why?easy
Solution
Step 1: Analyze Vehicle's state and behavior
Vehicle has common state (license plate) and behavior (park, unpark) shared by all types.Step 2: Abstract class vs interface for state and behavior
Abstract classes can hold state and provide default behavior; interfaces cannot hold instance state (Java 8+ default methods don't hold state). Thus, abstract class fits better.Final Answer:
Option B -> Option BQuick Check:
Shared state + behavior -> abstract class [check]
Hint: Stateful base class -> 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 the Meeting Scheduler, the
RoomBookingManager class has a bookRoom() method that checks if a room is available and then marks it booked. Without synchronizing bookRoom(), which exact sequence of events between Thread 1 and Thread 2 causes a double-booking race condition?medium
Solution
Step 1: Identify check-then-act gap
Without synchronization, Thread 1 reads availability as true but is preempted before booking; Thread 2 also reads true and proceeds.Step 2: Both threads book simultaneously causing double-booking
Because the availability check and booking are not atomic, both threads can book the same room concurrently.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
Hint: Check-then-act without sync -> double-booking race [check]
Common Mistakes:
5. The current Order Management System uses a
FeeStrategy interface for pricing. To add a WeekendFeeStrategy that applies extra fees on weekends, which minimal change respects SOLID principles?medium
Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options
Add a boolean weekend flag to Order and modify FeeStrategy implementations to check it. modifies existing classes violating OCP; Modify the existing FeeStrategy implementation to check for weekends and apply extra fees. also modifies existing code; Add WeekendFeeStrategy as a new class implementing FeeStrategy and configure Order to use it without modifying existing classes. adds new class but requires Order modification; Create a WeekendFeeDecorator that wraps existing FeeStrategy implementations and adds weekend fees. adds decorator without modifying existing classes, respecting OCP and DIP.Final Answer:
Option D -> Option DQuick Check:
Decorator adds behavior without modifying existing classes [check]
Hint: Decorator adds features without changing existing code [check]
Common Mistakes:
