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]
Vote class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance is created and accessed thread-safely.Step 2: Why Singleton fits Vote manager
Vote management requires a single point of control to maintain consistent vote counts and prevent conflicting updates, which Singleton enforces.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized static getInstance() -> Singleton [check]
getInstance() method that returns the single instance. Which design pattern is applied and why is this the CORRECT use case?Solution
Step 1: Identify the pattern from private constructor and static synchronized method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance is created lazily and safely in multithreaded contexts.Step 2: Eliminate other creational patterns
Factory controls creation but allows multiple instances; Builder separates construction steps; Prototype clones instances. None enforce single instance with thread safety.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> thread-safe Singleton [check]
WeekendFeeStrategy to calculate parking fees differently on weekends. According to SOLID principles, what is the minimum change required in the current Abstract Factory design?Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires new functionality added via extension, not modification.Step 2: Evaluate options
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it without modifying existing classes. respects OCP by adding a new class without modifying existing ones. Add a boolean weekend flag to ParkingLot and modify fee calculation accordingly. modifies ParkingLot, violating OCP. Options A and D modify existing classes, violating OCP.Final Answer:
Option A -> Option AQuick Check:
New strategy class added, existing classes unchanged [check]
parkVehicle() method checks if a spot is available before assigning it. Without synchronization, which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Trace check-then-act race
Thread 1 callsisAvailable()and sees true, but before assigning, context switches.Step 2: Thread 2 also calls
isAvailable()and sees true, then both assign the same spot concurrently.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act allows double assignment [check]
