Practice
Row abstraction for the in-memory SQL database, should Row be an abstract class or an interface, and why?Solution
Step 1: Analyze state and behavior requirements of Row
A Row holds column values (state) and provides methods to access or modify them (behavior). Interfaces cannot hold state in Java.Step 2: Choose between abstract class and interface
Abstract class can hold common state and provide default implementations, fitting Row's needs. Interface would force all implementations to duplicate state management.Final Answer:
Option B -> Option BQuick Check:
Row holds state -> abstract class needed [check]
parkVehicle() is not synchronized, which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Identify race condition in parkVehicle()
Without synchronization, two threads can both check spot availability before either marks it occupied.Step 2: Trace exact interleaving
Thread 1 calls isAvailable() -> true, context switch before marking occupied; Thread 2 calls isAvailable() -> true; both proceed to park, causing double-booking.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without synchronization causes double-booking [check]
makeMove() method, which exact sequence of thread interleaving causes two players to move simultaneously, violating turn order?Solution
Step 1: Identify the check-then-act race condition
Both threads read the turn state before either updates it, causing both to believe it's their turn.Step 2: Trace thread interleaving
Thread 1 checks turn -> true, context switch before move; Thread 2 checks turn -> true, both move simultaneously, violating turn order.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap allows double move without synchronization [check]
WeekendRetryPolicy to the Job Scheduler without modifying existing classes. Which SOLID principle guides the minimum change, and what is the correct approach?Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP to add WeekendRetryPolicy
Create a new class implementing RetryPolicy interface to add weekend-specific logic without modifying existing RetryPolicy or Job classes.Final Answer:
Option A -> Option AQuick Check:
New class implements interface -> no existing code changed [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclass ElectricCar extends Vehicle allows extension without changing existing class code.Step 2: Update compatibility map externally
Updating SpotCompatibility.COMPATIBILITY_MAP supports new types without modifying core logic, preserving OCP.Final Answer:
Option D -> Option DQuick Check:
Enum extension + subclass + external map update -> minimal change [check]
