Bird
Raised Fist0

Consider the following Python code using the Template Method Pattern with a hook method wants_sugar() that returns False in the subclass. What is the exact output when calling Tea().prepare()?

easy🧾 Trace Q3 of Q15
OOP & Design Patterns - Template Method Pattern - Define Skeleton, Override Steps
Consider the following Python code using the Template Method Pattern with a hook method wants_sugar() that returns False in the subclass. What is the exact output when calling Tea().prepare()?
ABoil water Steep tea Pour into cup
BBoil water Steep tea Add sugar Pour into cup
CBoil water Steep tea Add sugar
DSteep tea Boil water Pour into cup
Step-by-Step Solution
Solution:
  1. Step 1: Understand the hook method

    The hook wants_sugar() returns False, so sugar addition is skipped.
  2. Step 2: Follow the template method steps

    Boil water, steep tea, check hook (False), skip sugar, pour into cup.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Hook false means skip sugar step [OK]
Quick Trick: Hook false skips optional step; sugar not added [OK]
Common Mistakes:
MISTAKES
  • Assuming sugar is always added regardless of hook
  • Mixing order of steps in output
  • Ignoring the hook method's effect
Trap Explanation:
PITFALL
  • Assuming hook always returns True leads to incorrect output including sugar.
Interviewer Note:
CONTEXT
  • Tests ability to trace Template Method with hook methods.
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