Practice
1. In designing the ATM system, should the
Card entity be modeled as an abstract class or an interface, considering it has state (card number, expiry) and behavior (validate PIN)? Why?easy
Solution
Step 1: Analyze Card's characteristics
Card holds state (card number, expiry) and behavior (validate PIN), so it needs to share state and common logic.Step 2: Choose between abstract class and interface
Abstract class supports shared state and behavior inheritance. Interface cannot hold instance state, only constants and default methods without instance fields.Final Answer:
Option A -> Option AQuick Check:
Shared state + behavior -> abstract class [check]
Hint: Stateful base entity -> abstract class, not interface [check]
Common Mistakes:
2. A class provides a
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?easy
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]
Hint: Iterator hides structure, exposes traversal [check]
Common Mistakes:
3. You want to add a
WeekendFeeStrategy to calculate parking fees differently on weekends. According to SOLID principles, what is the minimum change required in the current Abstract Factory design?medium
Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires new functionality added via extension, not modification.Step 2: Evaluate options
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it without modifying existing classes. respects OCP by adding a new class without modifying existing ones. Add a boolean weekend flag to ParkingLot and modify fee calculation accordingly. modifies ParkingLot, violating OCP. Options A and D modify existing classes, violating OCP.Final Answer:
Option A -> Option AQuick Check:
New strategy class added, existing classes unchanged [check]
Hint: OCP: extend with new class, don't modify existing ones [check]
Common Mistakes:
4. Given the current parking lot design, adding a
WeekendFeeStrategy requires which minimum change to comply with SOLID principles?medium
Solution
Step 1: Identify the SOLID principle involved
The Open-Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Evaluate each option against OCP
Modify theFeeCalculatorclass to add weekend fee logic inside existing methods. and C modify existing classes violating OCP. ExtendFeeCalculatorwith a subclassWeekendFeeCalculatoroverriding fee methods. creates subclass but may cause code duplication. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without changing existing classes. adds a new strategy class implementingFeeStrategyand injects it, extending behavior without modifying existing code.Final Answer:
Option A -> Option AQuick Check:
New strategy class + injection -> OCP-compliant extension [check]
Hint: OCP: extend with new class, don't modify existing code [check]
Common Mistakes:
5. You need to add support for
ElectricCar that requires ChargingSpot compatibility in the current parking system. What is the minimum change to the design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
ElectricCar and ChargingSpot are new types requiring compatibility mapping without changing existing classes.Step 2: Apply minimal invasive change
Adding new enum values and subclass plus updating compatibility map extends functionality without modifying existing class code, preserving OCP.Step 3: Avoid violating OCP
Options B, C, and D modify existing classes or create parallel hierarchies, increasing complexity and violating OCP.Final Answer:
Option B -> Option BQuick Check:
Extend enums and map, subclass ElectricCar -> minimal change [check]
Hint: Extend enums and map, subclass new type -> minimal change [check]
Common Mistakes:
