Practice
Piece abstraction be an abstract class or an interface, and why?Solution
Step 1: Analyze shared state among pieces
All pieces share common state such as position on the board and color, which is best encapsulated in a base class.Step 2: Evaluate abstract class vs interface
Abstract classes allow shared state and common method implementations, while interfaces primarily declare behavior without state.Final Answer:
Option D -> Option DQuick Check:
Shared state -> abstract class preferred over interface [check]
Solution
Step 1: Understand OCP and DIP
OCP requires extending behavior without modifying existing code; DIP requires depending on abstractions, not concretions.Step 2: Evaluate options
Modify the existing FeeHandler class to add weekend fee logic inside its handleRequest() method. modifies existing class, violating OCP; Create a new WeekendFeeHandler implementing the FeeStrategy interface and add it to the chain without changing existing handlers. adds new handler, respecting OCP and DIP; Add a boolean flag isWeekend to FeeHandler and branch logic based on it. adds flag inside existing handler, violating OCP; Create a subclass WeekendFeeHandler extending FeeHandler and override handleRequest() with weekend logic. subclasses but may cause tight coupling and violates DIP if chain depends on concrete classes.Final Answer:
Option D -> Option DQuick Check:
New handler class added without modifying existing code [check]
sendNotification() method checks if a channel is available before sending. Without synchronizing this method, what exact sequence of thread interleaving causes duplicate notifications to be sent?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads can see the channel as available before either marks it busy.Step 2: Trace thread interleaving
Thread 1 checks availability -> true, context switch; Thread 2 checks availability -> true; both proceed to send, causing duplicate notifications.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act allows concurrent sends [check]
reserveSeat() method is not synchronized, which exact sequence of thread interleaving causes double-booking of the same seat?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads seat as available but is preempted before booking; Thread 2 also reads available and proceeds.Step 2: Both threads book the same seat concurrently
Because no lock or atomic operation protects the check-and-book sequence, double-booking occurs.Final Answer:
Option D -> Option DQuick 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]
