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 designing a plugin interface in Java, should the
Plugin be an abstract class or an interface if plugins must maintain internal state and provide default lifecycle methods? Why?easy
Solution
Step 1: Analyze state and default lifecycle method needs
Interfaces cannot hold instance state; abstract classes can hold fields and provide default method bodies.Step 2: Determine which construct fits stateful plugins with default behavior
Abstract class fits because it supports shared state and default lifecycle implementations, easing plugin development.Final Answer:
Option C -> Option CQuick Check:
Abstract class supports state and default methods [check]
Hint: State + default methods = abstract class, not interface [check]
Common Mistakes:
3. The auction system currently calculates fees with a fixed strategy. To add a
WeekendFeeStrategy without modifying existing classes, which SOLID principle is being applied and what is the minimum required change?medium
Solution
Step 1: Identify SOLID principle
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP by adding WeekendFeeStrategy implementing FeeStrategy interface without modifying existing classes.
Final Answer:
Option A -> Option AQuick Check:
New strategy class + injection -> OCP compliance [check]
Hint: Add new strategy class, no existing code change -> OCP [check]
Common Mistakes:
4. In a multi-threaded environment, if the
clone() method is not synchronized, which exact sequence of events can cause two threads to receive the same cloned object instance, leading to shared mutable state?medium
Solution
Step 1: Identify race condition in clone()
Without synchronization, clone() can copy partial or inconsistent state if interrupted mid-copy.Step 2: Trace exact interleaving causing shared mutable state
Thread 1 copies partial state, context switch to Thread 2 which copies same partial state, both return references to the same underlying mutable object.Final Answer:
Option B -> Option BQuick Check:
Unsynchronized clone() -> partial copy race -> shared mutable instance [check]
Hint: Unsync clone() -> partial copy race -> shared object [check]
Common Mistakes:
5. The system needs to support
ElectricCar playback devices that require ChargingSpot management. What is the minimum change to the current design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclassing allows extension without changing existing class code.Step 2: Update compatibility externally
Updating SpotCompatibility map outside existing classes respects OCP and avoids class explosion or god-class logic.Final Answer:
Option A -> Option AQuick Check:
Extend enums + subclass + external map update -> minimal change [check]
Hint: Extend enums + subclass + external map -> minimal change [check]
Common Mistakes:
