Practice
1. In a Card Game Framework, the
Deck class has a private constructor and a static synchronized getInstance() method returning the single deck instance. Which design pattern is applied here and why is this the correct choice?easy
Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Why Singleton fits this scenario
In a card game, having a single deck instance avoids inconsistencies and duplication, ensuring all players draw from the same deck state.Final Answer:
Option B -> Option BQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
2. In designing the Order class, should the
OrderState be an abstract class or an interface? Consider that each state has different behavior but shares no common state variables. Which is the best choice and why?easy
Solution
Step 1: Analyze state sharing needs
States have no shared variables, only behavior differences, so no need for shared state in a superclass.Step 2: Evaluate interface vs abstract class
Interface suits behavior contracts without state and allows flexibility; abstract class is unnecessary and restrictive here.Final Answer:
Option B -> Option BQuick Check:
Interface fits stateless behavior contracts best [check]
Hint: Interface for behavior-only contracts without shared state [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. To add a
WeekendFeeStrategy for pricing in the current product catalog design, which change respects SOLID principles with minimum modification?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: Why creating a new class is best
Implementing a newWeekendFeeStrategyclass extends functionality without modifying existing code, preserving OCP and avoiding side effects.Final Answer:
Option A -> Option AQuick Check:
New strategy class -> OCP compliance, no existing class changes [check]
Hint: OCP: extend with new class, don't modify existing [check]
Common Mistakes:
5. You need to add support for
ElectricCar and ChargingSpot in the card game framework's vehicle parking module. What is the minimum change to the current design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses extends functionality without changing existing code, preserving OCP.Step 2: External update of compatibility map
Updating the compatibility map externally allows new vehicle and spot types to integrate without altering existing class logic.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + external map update -> no existing class modified [check]
Hint: New enums + subclass + external map -> extend without modify [check]
Common Mistakes:
