Bird
Raised Fist0

Given a decorator chain where the final cost returned is 8, and you know the base object cost is 5, and two decorators add 1 and 2 respectively, which decorator was applied last?

hard🔄 Reverse Engineer Q9 of Q15
OOP & Design Patterns - Decorator Pattern - Wrapping Behaviour Without Subclassing
Given a decorator chain where the final cost returned is 8, and you know the base object cost is 5, and two decorators add 1 and 2 respectively, which decorator was applied last?
AThe decorator adding 2 was applied last
BThe decorator adding 1 was applied last
CBoth decorators were applied simultaneously
DCannot determine from given information
Step-by-Step Solution
Solution:
  1. Step 1: Calculate total cost

    Base cost 5 + 1 + 2 = 8 matches final cost.
  2. Step 2: Understand decorator call order

    Last applied decorator adds its cost last in the call chain.
  3. Step 3: Identify last decorator

    Since final cost includes +2 last, decorator adding 2 was applied last.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Decorator call order affects cost accumulation order [OK]
Quick Trick: Last decorator's cost added last in chain -> final increment [OK]
Common Mistakes:
MISTAKES
  • Assuming order doesn't affect cost
  • Confusing first and last decorator
  • Ignoring additive nature of decorators
Trap Explanation:
PITFALL
  • Candidates often ignore that decorator order affects final cost calculation.
Interviewer Note:
CONTEXT
  • Tests reasoning backwards through decorator call chain.
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