Practice
LegacyRectangleAdapter that implements the Shape interface by internally using a LegacyRectangle class with incompatible methods. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the role of LegacyRectangleAdapter
The adapter wraps LegacyRectangle to provide the Shape interface expected by clients, enabling incompatible interfaces to work together.Step 2: Differentiate from other patterns
Decorator adds behavior without changing interface, Facade simplifies but does not adapt interfaces, Proxy controls access. Only Adapter converts interfaces.Final Answer:
Option B -> Option BQuick Check:
Adapter converts incompatible interfaces to expected ones [check]
Vehicle abstraction that requires storing state like speed and fuelLevel, and behavior like accelerate(), should Vehicle be an abstract class or an interface? Why?Solution
Step 1: Analyze state and behavior requirements
Vehicle needs to hold shared mutable state likespeedandfuelLevel, which interfaces cannot hold as fields.Step 2: Evaluate abstract class vs interface
Abstract classes can have instance variables and default method implementations, enabling code reuse and shared state. Interfaces cannot hold instance state, only constants.Final Answer:
Option A -> Option AQuick Check:
Stateful abstraction -> abstract class, not interface [check]
WeekendFeeStrategy to the traffic light system's parking fee calculation. According to SOLID principles, what is the minimum change required?Solution
Step 1: Identify applicable SOLID principle
Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Evaluate options for OCP compliance
Adding a flag with conditional logic violates OCP by modifying existing class; creating a new strategy class and injecting it respects OCP.Final Answer:
Option A -> Option AQuick Check:
New strategy class + injection -> OCP compliant extension [check]
placeBid() method checks if the auction is still open before accepting a bid. Without synchronizing placeBid(), which exact thread interleaving causes two threads to accept bids after the auction closes, resulting in inconsistent state?Solution
Step 1: Identify check-then-act race
Without synchronization, Thread 1 reads auction open as true but is preempted before recording the bid.Step 2: Thread 2 reads auction open as true and proceeds to record bid as well, both bypassing the closed state check.
Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double acceptance after close [check]
selectItem() is not synchronized, which exact thread interleaving causes dispensing the same item twice?Solution
Step 1: Identify the check-then-act race
Without synchronization, both threads can read item availability as true before either decrements inventory.Step 2: Trace the interleaving
Thread 1 checks availability -> true, context switch; Thread 2 checks availability -> true; both proceed to dispense, causing double dispense.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double dispense [check]
