Practice
1. A logging framework has a
Logger class with a private constructor and a static synchronized getInstance() method returning the single instance. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify the pattern from private constructor and static getInstance()
The private constructor prevents external instantiation, and the static synchronized getInstance() ensures a single shared instance, which matches the Singleton pattern.Step 2: Why Singleton is correct here
Logging frameworks require a single global Logger instance to coordinate log output consistently. Factory, Builder, and Prototype patterns do not enforce single instance semantics.Final Answer:
Option D -> Option DQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
2. In a messaging system design, the
Conversation class has a private constructor and a static synchronized getInstance() method that returns the single instance per conversation ID. Which design pattern is applied here and why is this the correct use case?easy
Solution
Step 1: Identify the pattern from constructor and method
The private constructor and static synchronizedgetInstance()method indicate Singleton pattern usage, controlling instance creation.Step 2: Understand why Singleton fits this use case
Singleton ensures only one Conversation instance per conversation ID, preventing duplicate conversation objects and maintaining consistent state.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
3. In designing an Observer interface in Java, should it be an abstract class or an interface, and why? Consider that observers only define a single method
update() without shared state.easy
Solution
Step 1: Analyze Observer requirements
Observers only defineupdate()without shared state, so no need for instance variables or default behavior.Step 2: Choose interface for flexibility
Interfaces allow multiple inheritance and keep observers lightweight; abstract classes add unnecessary state and restrict inheritance.Final Answer:
Option B -> Option BQuick Check:
Observer interface with single method and no state -> interface preferred [check]
Hint: Observer has no state, so interface enables flexible multiple inheritance [check]
Common Mistakes:
4. Given a
Vehicle abstraction that requires both state (like speed) and behavior (like accelerate), should Vehicle be an abstract class or an interface in Java, and why?easy
Solution
Step 1: Analyze state and behavior requirements
Vehicle needs to hold state like speed and provide behavior like accelerate with shared code, which requires fields and method implementations.Step 2: Compare abstract class vs interface capabilities
Abstract classes can have instance variables and concrete methods; interfaces cannot hold instance state (only constants) and default methods cannot maintain state.Final Answer:
Option C -> Option CQuick Check:
State + behavior with shared code -> abstract class [check]
Hint: State needs fields? Use abstract class, not interface [check]
Common Mistakes:
5. In the auction system, the
placeBid() method checks if the auction is still open before accepting a bid. Without synchronizing placeBid(), which exact thread interleaving causes two threads to accept bids after the auction closes, resulting in inconsistent state?medium
Solution
Step 1: Identify check-then-act race
Without synchronization, Thread 1 reads auction open as true but is preempted before recording the bid.Step 2: Thread 2 reads auction open as true and proceeds to record bid as well, both bypassing the closed state check.
Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double acceptance after close [check]
Hint: Check-then-act without sync -> race condition double accept [check]
Common Mistakes:
