Practice
Vehicle be an abstract class or an interface, and why?Solution
Step 1: Analyze Vehicle's shared state and behavior
Vehicle has common fields (e.g., licensePlate) and methods (e.g., startEngine) that require shared implementation and state storage.Step 2: Abstract class vs interface suitability
Abstract classes allow shared state and method implementations, while interfaces cannot hold instance state. Thus, abstract class fits Vehicle better.Final Answer:
Option A -> Option AQuick Check:
Shared state + behavior -> abstract class [check]
bookSeat() method is not synchronized, which exact sequence of events between Thread 1 and Thread 2 causes double-booking of the same seat?Solution
Step 1: Trace Thread 1's check-then-act gap
Thread 1 reads seat as available but is preempted before marking it booked.Step 2: Thread 2 also reads seat as available and proceeds to book, causing double-booking.
Without synchronization, both threads act on stale availability state.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without lock -> double-booking [check]
loadPlugin() method checks if a plugin is already loaded before loading it. Without synchronizing this method, what exact thread interleaving causes the same plugin to be loaded twice?Solution
Step 1: Trace the check-then-act sequence without synchronization
Thread 1 calls isLoaded() and sees false, then context switches before loading.Step 2: Thread 2 also calls isLoaded() and sees false, both threads proceed to load plugin concurrently, causing duplicate loading.
Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act allows double loading due to race [check]
parkVehicle() is NOT synchronized, which exact sequence causes double-booking of the same spot?Solution
Step 1: Identify the race condition
Without synchronization, both threads can read spot as available before either marks it occupied.Step 2: Trace exact interleaving
Thread 1: isAvailable() -> true, context switch before marking occupied; Thread 2: isAvailable() -> true, both proceed to park, causing double-booking.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
ElectricCar vehicles requiring ChargingSpot parking. What is the minimum change to the current Java design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot must not modify existing classes to respect OCP and avoid class explosion.Step 2: Minimal change approach
Adding new enum values and creating new subclasses for ElectricCar and ChargingSpot, plus updating compatibility externally, extends functionality without changing existing code.Final Answer:
Option A -> Option AQuick Check:
New enums + subclasses + external compatibility update -> no existing class change [check]
