Bird
Raised Fist0

When an object composed of multiple behaviors receives a request to perform an action, what is the typical sequence of internal steps that occur to fulfill this request?

easy🧠🧾 Concept Trace Q12 of Q15
OOP & Design Patterns - Composition vs Inheritance - Favour Composition, Why & When
When an object composed of multiple behaviors receives a request to perform an action, what is the typical sequence of internal steps that occur to fulfill this request?
AThe object delegates the action to one or more composed behavior objects which execute their respective parts
BThe object checks flags internally and runs conditional code for each behavior
CThe object directly executes the action code inherited from its superclass
DThe object creates a new subclass instance dynamically to handle the action
Step-by-Step Solution
  1. Step 1: Understand delegation in composition

    In composition, the main object delegates responsibilities to composed behavior objects rather than handling all logic itself.
  2. Step 2: Analyze each option

    The object directly executes the action code inherited from its superclass describes inheritance, not composition. The object checks flags internally and runs conditional code for each behavior implies flag-based conditional logic, which is less flexible. The object creates a new subclass instance dynamically to handle the action is not a typical or practical approach.
  3. Step 3: Confirm correct flow

    The composed behaviors receive the delegated call and execute their specific logic, enabling modular and maintainable design.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Delegation to composed objects is the hallmark of composition-based design.
Quick Trick: Composition means delegation to behavior objects, not direct inheritance execution.
Common Mistakes:
MISTAKES
  • Confusing inheritance method calls with composition delegation
  • Assuming flags control behavior execution internally
Trap Explanation:
PITFALL
  • Delegation is subtle; candidates often think the main object handles everything or uses flags internally.
Interviewer Note:
CONTEXT
  • Tests understanding of how composition delegates responsibilities at runtime.
Master "Composition vs Inheritance - Favour Composition, Why & When" 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