Practice
1. In a notification system, a
NotificationFactory class has a static method that returns different Notification subclasses (e.g., EmailNotification, SMSNotification) based on input parameters. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify the role of NotificationFactory
The factory method returns different Notification subclasses based on input parameters, encapsulating object creation logic.Step 2: Eliminate other patterns
Builder is for stepwise construction, Singleton ensures single instance, Decorator adds behavior dynamically; none match the described creation logic.Final Answer:
Option B -> Option BQuick Check:
Factory pattern encapsulates subclass instantiation based on input [check]
Hint: Factory creates objects based on input type [check]
Common Mistakes:
2. In the Elevator System, should the
Elevator class implement an interface or extend an abstract class to represent different elevator types (e.g., passenger, freight)? Consider that elevators share common state and behavior but also have type-specific logic.easy
Solution
Step 1: Analyze shared state and behavior
Elevators share common fields like currentFloor, direction, and doorStatus, plus some implemented methods.Step 2: Abstract class fits shared state and partial behavior
Abstract class allows code reuse and enforces common state, while subclasses override type-specific logic.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and behavior [check]
Hint: Abstract class for shared state; interface for pure behavior [check]
Common Mistakes:
3. The Wallet System currently calculates fees with a
FeeStrategy interface. To add a WeekendFeeStrategy without modifying existing classes, which SOLID principle is being applied and what is the minimum required change?medium
Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP to fee calculation
Adding WeekendFeeStrategy as a new class implementing FeeStrategy extends behavior without modifying existing classes, preserving OCP.Final Answer:
Option A -> Option AQuick Check:
New FeeStrategy implementation -> no existing class changes -> OCP [check]
Hint: Add new strategy class, no existing code change -> OCP [check]
Common Mistakes:
4. Adding a
WeekendFeeStrategy to the current parking fee system requires which minimum change to comply with SOLID principles?medium
Solution
Step 1: Identify applicable SOLID principle
The Open/Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a newWeekendFeeStrategyclass implementingFeeStrategywithout modifying existing classes. respects OCP by adding a new class without modifying existing ones. Add a boolean flagisWeekendinParkingLotand conditionally apply fees inside existing strategies. violates OCP by modifyingParkingLotand existing strategies with conditional logic.Final Answer:
Option A -> Option AQuick Check:
New strategy class added, existing code unchanged -> OCP respected [check]
Hint: Add new strategy class, don't modify existing ones [check]
Common Mistakes:
5. ElectricCar requires ChargingSpots in the parking lot system. What is the minimum change to support this without modifying existing classes?
hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and a subclass extends functionality without changing existing class code, respecting OCP.Step 2: Evaluate options
ModifyVehicleclass to include achargingRequiredflag and update all spot checks. modifies Vehicle, violating OCP. Create a newChargingSpotclass and modifyParkingLotto handle it specially. requires modifying ParkingLot, violating minimal change. Add a new interfaceChargeableand makeElectricCarimplement it, changing existing spot logic. adds interface but requires changing spot logic, violating minimal change.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + compatibility map update -> minimal change [check]
Hint: Add enums + subclass + compatibility map, no existing class change [check]
Common Mistakes:
