Practice
Facade provides a simplified interface to a complex subsystem by delegating calls to multiple internal classes. It hides subsystem details from clients. Which design pattern is this an example of, and why is it the correct choice here?Solution
Step 1: Identify the role of the Facade class
The Facade class simplifies client interaction by exposing a single interface that delegates to multiple subsystem classes, hiding complexity.Step 2: Differentiate from similar patterns
Decorator adds behavior dynamically, Adapter changes interface, Proxy controls access; none simplify multiple interfaces into one.Final Answer:
Option A -> Option AQuick Check:
Facade unifies subsystem interfaces -> simplified client interaction [check]
createIterator() method that returns an object allowing traversal over its elements without exposing its internal data structure. Which design pattern is being applied and why is this the correct choice?Solution
Step 1: Identify the role of
This method returns an object that allows clients to traverse elements without exposing the collection's internal representation, which is the core intent of the Iterator pattern.createIterator()Step 2: Eliminate other patterns
Factory Method focuses on object creation, Decorator adds responsibilities dynamically, and Composite manages tree structures. None focus on traversal abstraction.Final Answer:
Option C -> Option CQuick Check:
Iterator separates traversal from collection internals [check]
Hand class has a method addCard(Card card) that is not synchronized. Two threads concurrently add cards to the same hand. Which exact sequence causes a race condition leading to lost cards?Solution
Step 1: Identify the check-then-act gap
Without synchronization, both threads read the current size of the card list before either adds their card.Step 2: Trace interleaving causing overwrite
Thread 1 reads size N, context switches; Thread 2 reads size N; both add at index N, causing one card to overwrite the other.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized addCard -> check-then-act race -> lost card [check]
Board class has a placePiece() method that first checks if a position is free and then places the piece. Without synchronizing placePiece(), what exact sequence of thread interleaving causes two threads to place different pieces at the same position (double-booking)?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 checks position free and sees true, but before placing, context switches.Step 2: Thread 2 also checks position free -> true and proceeds to place piece concurrently, causing double-booking.
Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act allows both threads to place pieces at same position [check]
ElectricCar and ChargingSpot in the Chess game parking lot design, what is the minimum change that respects extensibility without modifying existing classes?Solution
Step 1: Identify extensibility requirements
New vehicle and spot types must be added without modifying existing classes to respect OCP.Step 2: Evaluate Java enum and class extension approach
Adding new enum constants and creating new subclasses externally allows extension without modifying existing code. Updating compatibility map externally avoids class explosion.Final Answer:
Option C -> Option CQuick Check:
Extends enums and classes externally, no existing class changes [check]
