Practice
1. In the Restaurant Management System, should the
MenuItem be designed as an abstract class or an interface? Consider that MenuItem has shared state like price and description, and behavior like calculateDiscount().easy
Solution
Step 1: Analyze state and behavior requirements
MenuItem has shared state (price, description) and behavior (calculateDiscount), which interfaces cannot hold as fields.Step 2: Abstract class supports shared state and default behavior
Abstract classes can have instance variables and method implementations, making them suitable for MenuItem.Final Answer:
Option B -> Option BQuick Check:
Shared state + behavior -> abstract class [check]
Hint: Stateful base class -> abstract class, stateless contract -> interface [check]
Common Mistakes:
2. In designing the vending machine's product selection module, should
Product be modeled as an abstract class or an interface? Consider that products have shared state like price and inventory count, but different behaviors like dispense() and getDescription().easy
Solution
Step 1: Analyze shared state and behavior
Products share common state like price and inventory count, which requires fields and possibly default implementations.Step 2: Choose between interface and abstract class
Abstract classes can hold state and provide default method implementations, while interfaces cannot hold instance state (only constants).Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and behavior [check]
Hint: Abstract class holds state; interface only behavior signatures [check]
Common Mistakes:
3. A class has a private constructor and a static synchronized
getInstance() method returning a single instance. Which pattern is applied and why is this the CORRECT use case?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 with thread safety.Step 2: Eliminate other creational patterns
Factory Method creates objects but doesn't enforce single instance; Builder constructs complex objects but not singleton; Prototype clones instances, not restricts to one.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized static getter -> Singleton pattern [check]
Hint: Private constructor + static synchronized getter -> Singleton [check]
Common Mistakes:
4. You want to add a
WeekendBonusRule that gives players extra moves on weekends without modifying existing classes. Which SOLID principle guides the minimal change, and what is the correct approach?medium
Solution
Step 1: Identify SOLID principle for extension without modification
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Evaluate approaches
Subclassing Player to WeekendPlayer allows extension without modifying Player; injecting new MoveRule class also respects OCP but requires interface design.Final Answer:
Option C -> Option CQuick Check:
Subclassing extends behavior without modifying base class [check]
Hint: Extend via subclass or interface, don't modify existing classes [check]
Common Mistakes:
5. The vending machine currently applies a flat pricing strategy. To add a
WeekendFeeStrategy that charges extra on weekends, which minimal change respects SOLID principles?medium
Solution
Step 1: Identify the SOLID principle involved
The Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a newWeekendFeeStrategyclass implementingPricingStrategyand inject it without modifying existing classes. adds a new strategy class without modifying existing code, respecting OCP. Others modify existing classes or mix concerns.Final Answer:
Option A -> Option AQuick Check:
New strategy class extends pricing without modifying existing code [check]
Hint: OCP: extend pricing via new class, don't modify existing [check]
Common Mistakes:
