Practice
Button and Checkbox families without specifying their concrete classes. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the role of the factory class
The factory creates multiple related objects (Button, Checkbox) without exposing their concrete classes, indicating a family of products.Step 2: Match pattern intent
Abstract Factory pattern is designed to create families of related objects without specifying their concrete classes, unlike Builder (stepwise construction), Singleton (single instance), or Prototype (cloning).Final Answer:
Option C -> Option CQuick Check:
Abstract Factory creates related object families without concrete classes [check]
AuctionManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct choice?Solution
Step 1: Identify Singleton intent
The private constructor and static synchronizedgetInstance()method ensure only one instance ofAuctionManagerexists, centralizing auction state management.Step 2: Eliminate other patterns
Factory creates multiple objects, Builder incrementally builds complex objects, Prototype clones instances--none enforce single global instance.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Vehicle abstraction, should it be an abstract class or an interface if it needs to hold common state like speed and fuelLevel? Why?Solution
Step 1: Analyze state and behavior requirements
SinceVehicleneeds to hold common state likespeedandfuelLevel, it requires instance variables.Step 2: Choose abstraction supporting state
Abstract classes can have instance variables and shared method implementations, whereas interfaces cannot hold instance state (only constants and default methods without state).Final Answer:
Option B -> Option BQuick Check:
State requires instance variables -> abstract class [check]
PrivacyStrategy interface for post visibility. To add a WeekendPrivacyStrategy that restricts posts only on weekends, what is the minimum change required to comply with SOLID principles?Solution
Step 1: Identify applicable SOLID principle
Open/Closed Principle (OCP) requires extending behavior without modifying existing code.Step 2: Apply OCP to privacy strategies
Adding a new class implementingPrivacyStrategyextends functionality without changing existing classes.Final Answer:
Option B -> Option BQuick Check:
New class for new behavior -> OCP compliance [check]
ElectricCar and ChargingSpot in the Wallet System's parking module, what is the minimum change to the current design to maintain extensibility without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses externally allows extension without changing existing code, preserving OCP.Step 2: Use external compatibility map update
Updating SpotCompatibility.COMPATIBILITY_MAP externally avoids modifying existing classes, enabling new vehicle and spot types to integrate cleanly.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + external map update -> no existing class changes [check]
