Practice
Card abstraction, should it be an abstract class or an interface? Consider that cards have immutable state (suit, rank) and common behavior like compareTo(). Which is the best choice and why?Solution
Step 1: Analyze state and behavior requirements
Cards have immutable state (suit, rank) and common behavior likecompareTo()that can be implemented once.Step 2: Abstract class vs interface capabilities
Abstract classes can hold state and provide method implementations, while interfaces (pre-Java 8) cannot hold state and only declare methods.Final Answer:
Option C -> Option CQuick Check:
Immutable state + shared behavior -> abstract class [check]
final method that calls several abstract methods which subclasses must implement. Which design pattern is this an example of?Solution
Step 1: Identify the role of the final method
The base class'sfinalmethod defines the fixed algorithm skeleton, preventing subclasses from changing the overall flow.Step 2: Recognize abstract methods as extension points
Subclasses implement abstract methods to fill in specific steps, matching the Template Method pattern's intent.Final Answer:
Option A -> Option AQuick Check:
Base class controls algorithm flow; subclasses customize steps [check]
getInstance() method returning the single instance. Which pattern is applied and why is this the CORRECT use case?Solution
Step 1: Identify Singleton pattern characteristics
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance with thread-safe lazy initialization.Step 2: Eliminate other creational patterns
Factory Method controls subclass instantiation, Builder separates construction from representation, and Prototype clones objects; none enforce single instance or thread safety.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Executor class's executeStep() method is not synchronized, which exact thread interleaving causes two threads to execute the same step concurrently, leading to inconsistent state?Solution
Step 1: Identify the check-then-act race
Without synchronization, Thread 1 checks step status as free, then context switches before marking running.Step 2: Thread 2 repeats check before Thread 1 marks running
Thread 2 also sees step free and proceeds, causing both to execute concurrently.Final Answer:
Option C -> Option CQuick Check:
Unsynchronized check-then-act -> double execution race [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Enums VehicleType and SpotType can be extended with new constants without changing existing class logic. Creating ElectricCar subclass extends Vehicle cleanly.Step 2: Update SpotCompatibility map externally
Maintaining compatibility mapping outside existing classes avoids modifying them, preserving OCP and avoiding class explosion.Final Answer:
Option A -> Option AQuick Check:
Extend enums + subclass + external compatibility map -> minimal change [check]
