Bird
Raised Fist0

Consider the Template Method Pattern with a hook method that returns False to skip condiments. What is the output of Coffee().prepare_recipe() if customer_wants_condiments() returns False?

medium🧾 Code Trace Q4 of Q15
OOP & Design Patterns - Template Method Pattern - Define Skeleton, Override Steps
Consider the Template Method Pattern with a hook method that returns False to skip condiments. What is the output of Coffee().prepare_recipe() if customer_wants_condiments() returns False?
ABoil water Brew coffee grounds Pour into cup Add sugar and milk
BBoil water Brew coffee grounds Pour into cup
CBrew coffee grounds Boil water Pour into cup
DBoil water Pour into cup Brew coffee grounds
Step-by-Step Solution
Solution:
  1. Step 1: Trace prepare_recipe with hook returning False

    Boil water, brew, pour in cup called; add_condiments skipped.
  2. Step 2: Substitute subclass methods

    brew() prints 'Brew coffee grounds', add_condiments() skipped due to hook.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Condiments skipped correctly when hook returns False [OK]
Quick Trick: Hook method controls optional steps [OK]
Common Mistakes:
MISTAKES
  • Forgetting hook disables condiment step.
Trap Explanation:
PITFALL
  • Candidates often assume add_condiments always runs, ignoring hook method effect.
Interviewer Note:
CONTEXT
  • Tests edge case handling with hook method in Template Method.
Master "Template Method Pattern - Define Skeleton, Override Steps" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes