Bird
Raised Fist0

Given the following code snippet using the Decorator Pattern, what is the output of print(coffee.description())?

easy🧾 Code Trace Q3 of Q15
OOP & Design Patterns - Decorator Pattern - Wrapping Behaviour Without Subclassing
Given the following code snippet using the Decorator Pattern, what is the output of print(coffee.description())? ```python class SimpleCoffee: def description(self): return "Coffee" class MilkDecorator: def __init__(self, coffee): self._coffee = coffee def description(self): return self._coffee.description() + ", Milk" coffee = MilkDecorator(SimpleCoffee()) print(coffee.description()) ```
ACoffee
BMilk, Coffee
CCoffee, Milk
DMilk
Step-by-Step Solution
Solution:
  1. Step 1: Trace coffee.description()

    MilkDecorator.description() calls SimpleCoffee.description() which returns "Coffee".
  2. Step 2: Append decorator behavior

    MilkDecorator adds ", Milk" to the description, resulting in "Coffee, Milk".
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Decorator appends feature description correctly [OK]
Quick Trick: Decorator calls wrapped object's method then adds behavior [OK]
Common Mistakes:
MISTAKES
  • Reversing order of concatenation
  • Forgetting to call wrapped object's method
  • Returning only decorator's string
Trap Explanation:
PITFALL
  • Candidates often confuse order or forget delegation to wrapped object.
Interviewer Note:
CONTEXT
  • Tests ability to trace decorator method calls and string concatenation.
Master "Decorator Pattern - Wrapping Behaviour Without Subclassing" 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