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]
Vehicle abstraction that requires both state (e.g., speed) and behavior (e.g., accelerate()), should Vehicle be an abstract class or an interface in Java, and why?Solution
Step 1: Analyze state and behavior requirements
SinceVehicleneeds to hold state like speed and provide default behavior likeaccelerate(), it requires fields and method implementations.Step 2: Choose between abstract class and interface
Java interfaces cannot hold instance state (fields) prior to Java 8 default methods, and even then, no instance variables. Abstract classes can hold state and provide default method implementations, making them suitable here.Final Answer:
Option C -> Option CQuick Check:
State + default behavior -> abstract class, not interface [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]
addProductToCart() method is not synchronized, which exact sequence of events can cause the cart to contain duplicate entries for the same product?Solution
Step 1: Identify check-then-act race condition
Both threads check if product exists and see false before either adds it, due to lack of synchronization.Step 2: Both threads add product concurrently causing duplicates
Because the add operation is not atomic, both add the product, resulting in duplicates.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without synchronization -> duplicate entries [check]
parkVehicle() is not synchronized, which exact thread interleaving 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 and is preempted before marking it occupied. Thread 2 also reads available and proceeds to park.Step 2: Confirm both threads park concurrently causing double-booking
Both threads believe the spot is free and park, leading to inconsistent state and double-booking.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
