Practice
Command interface in Java, should it be an abstract class or an interface, and why? Consider that commands may need to store state for undo functionality.Solution
Step 1: Analyze state requirements for undo
Commands often need to store state (e.g., previous values) to support undo, which requires fields.Step 2: Compare interface vs abstract class in Java
Interfaces cannot hold instance fields, so abstract classes are better to share common state and default undo logic.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and behavior for undo [check]
MeetingManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here, and why is this the correct choice for managing meeting schedules?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: Reason why Singleton fits MeetingManager
MeetingManager must coordinate all meetings centrally to avoid conflicting schedules; multiple instances could cause inconsistent state.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
User class has a private constructor and a static synchronized getInstance() method to ensure only one instance per user ID exists. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the pattern from constructor and method
The private constructor and static synchronizedgetInstance()method indicate Singleton usage to control instance creation.Step 2: Why Singleton fits here
Singleton ensures only one User instance per user ID exists, preventing duplicate objects and managing concurrency safely.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Vehicle that provides a template method for starting the engine and driving, but also maintains internal state like fuel level. Should Vehicle be an abstract class or an interface, and why?Solution
Step 1: Consider state management
SinceVehiclemaintains internal state like fuel level, it requires fields, which interfaces cannot hold.Step 2: Evaluate method implementations
Abstract classes can provide default implementations for template methods, supporting the Template Method pattern.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports state and default behavior [check]
parkVehicle(), which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Identify check-then-act race condition
Without synchronization, both threads read spot as available before either marks it occupied.Step 2: Trace interleaving
Thread 1 reads availability true, context switch; Thread 2 reads availability true; both proceed to park, causing double-booking.Final Answer:
Option C -> Option CQuick Check:
Unsynchronized check-then-act allows both threads to park same spot [check]
