Bird
Raised Fist0

Examine the following buggy code implementing the Template Method Pattern. Which line contains the subtle bug that breaks the pattern's intended behavior?

medium🐞 Bug Identification Q14 of Q15
OOP & Design Patterns - Template Method Pattern - Define Skeleton, Override Steps
Examine the following buggy code implementing the Template Method Pattern. Which line contains the subtle bug that breaks the pattern's intended behavior?
ALine overriding prepare_recipe in Tea subclass
BLine defining abstract method brew in base class
CLine calling add_condiments inside prepare_recipe base method
DLine overriding customer_wants_condiments in Tea subclass
Step-by-Step Solution
  1. Step 1: Identify overridden methods

    Tea overrides prepare_recipe, which breaks the template method pattern by duplicating and changing the algorithm flow.
  2. Step 2: Understand impact

    Overriding the template method in subclass bypasses the base class skeleton, causing inconsistent behavior and code duplication.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Template method must not be overridden by subclasses [OK]
Quick Trick: Overriding template method breaks algorithm skeleton [OK]
Common Mistakes:
MISTAKES
  • Thinking overriding abstract methods is bug
  • Ignoring hook method usage
Trap Explanation:
PITFALL
  • Other lines look suspicious but are correct; overriding template method is subtle but critical bug.
Interviewer Note:
CONTEXT
  • Tests if candidate knows correct usage of Template Method pattern and common pitfalls.
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