Practice
Solution
Step 1: Understand the requirement for families of related objects
The problem states the need to create related vehicles (e.g., electric car and electric bike) that belong to a family and must be consistent.Step 2: Match pattern to requirement
Abstract Factory is designed to create families of related objects, ensuring that the created objects are compatible and consistent.Final Answer:
Option B -> Option BQuick Check:
Abstract Factory creates related object families, Factory creates single objects [OK]
- Confusing Factory with Abstract Factory
- Using Builder for simple object creation
Tea().prepare_recipe()?Solution
Step 1: Trace prepare_recipe steps
Calls boil_water (prints 'Boil water'), then brew (prints 'Steep tea bag'), then pour_in_cup (prints 'Pour into cup').Step 2: Check hook method customer_wants_condiments
Tea overrides it to return False, so add_condiments is skipped.Final Answer:
Option A -> Option AQuick Check:
Condiments skipped due to hook returning False [OK]
- Assuming condiments always added
- Mixing order of steps
Solution
Step 1: Interfaces and state
Interfaces cannot hold instance fields; they only declare constants (static final fields).Step 2: Abstract classes and state
Abstract classes can have constructors and instance variables to maintain state.Step 3: Default methods in interfaces
Modern languages allow default method implementations in interfaces.Step 4: Partial implementation in abstract classes
Abstract classes can provide some method implementations while leaving others abstract.Final Answer:
Option A -> Option AQuick Check:
Interfaces cannot hold mutable state; abstract classes can.
- Assuming interfaces can have instance variables.
- Confusing default methods with full implementations.
- Believing abstract classes cannot have constructors.
Solution
Step 1: Analyze cost() method
cost() calls self.cost() recursively, causing infinite recursion instead of delegating to wrapped object.Step 2: Identify correct delegation
Should call super().cost() to delegate to wrapped coffee object's cost method.Final Answer:
Option A -> Option AQuick Check:
Recursive call causes stack overflow, breaking decorator chain [OK]
- Calling self.cost() instead of super().cost() in decorators
- Forgetting to delegate calls properly
Solution
Step 1: Embedding snakes/ladders inside Board
This approach simplifies initial design but tightly couples Board to these elements.Step 2: Impact on extensibility
Tightly coupled design makes it harder to add new board elements (e.g., portals, traps) without modifying Board.Step 3: Modeling as separate entities
Allows easy extension by adding new element types without changing Board internals.Step 4: Complexity and memory considerations
Separate entities add minimal overhead; runtime complexity impact is negligible.Final Answer:
Option B -> Option BQuick Check:
Trade-off is between simplicity and extensibility, not runtime complexity or memory bloat.
- Thinking separate entities cause big runtime overhead
- Believing embedding improves encapsulation and immutability
- Assuming separate entities cause memory bloat
