Practice
Command interface declares an execute() method and concrete commands implement this interface to encapsulate operations. Which OOP concept is primarily applied here and why is this the correct use case?Solution
Step 1: Identify the role of Command objects
Each command encapsulates all information needed to perform an action, hiding the operation's details from the invoker.Step 2: Differentiate encapsulation from other concepts
While polymorphism and abstraction are involved, the key concept is encapsulation because commands package the action and its parameters as an object.Final Answer:
Option A -> Option AQuick Check:
Command objects encapsulate operations and data for undo/redo [check]
RetryPolicy be an abstract class or an interface, and why?Solution
Step 1: Analyze RetryPolicy requirements
RetryPolicy typically holds state such as retry count, max retries, and backoff strategy, which benefits from shared code and fields.Step 2: Abstract class vs interface in Java
Abstract classes can hold state and provide default implementations, while interfaces (pre-Java 8) cannot hold state. Since RetryPolicy needs state, abstract class fits better.Final Answer:
Option D -> Option DQuick Check:
RetryPolicy holds state -> abstract class [check]
FileSystem class has a private constructor and a static synchronized getInstance() method returning the single instance. Which pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method signature
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Reason why Singleton fits this use case
Singleton is appropriate because the file system state must be globally consistent and unique; multiple instances would cause conflicting states.Final Answer:
Option B -> Option BQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
Player abstraction for the Tic-Tac-Toe game, should Player be an abstract class or an interface, and why?Solution
Step 1: Analyze state and behavior requirements
Players have common state (e.g., name, symbol, score) and some shared behavior (e.g., basic validation). Abstract classes allow state fields and shared method implementations.Step 2: Why abstract class is better than interface here
Interfaces define behavior contracts but cannot hold instance state. Although Java 8+ interfaces support default methods, they cannot hold mutable state. Thus, abstract class suits better for shared state and behavior.Final Answer:
Option C -> Option CQuick Check:
Shared state + behavior -> abstract class, not interface [check]
parkVehicle() is not synchronized, which exact sequence of Thread 1 and Thread 2 causes double-booking of the same spot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available but before marking it occupied, Thread 2 also reads it as available.Step 2: Both threads proceed to assign the same spot concurrently
Because no lock protects the check and assignment, both parkVehicle calls succeed, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
