QuizzesComposition over Inheritance — Concept Check
intermediate
lld

Composition over Inheritance — Concept Check

Questions on Composition over Inheritance. Understand why has-a relationships are more flexible than is-a, illustrated by the classic Duck problem refactored with FlyBehavior Strategy.

8 minutes 8 questions Pass at 70%
Time Limit
8 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

8 questions on Composition over Inheritance — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.In a Duck simulation, a RubberDuck inherits from Duck and has to override the fly() method to do nothing. What design principle is being violated here?

    • ARubberDuck should not override methods
    • BComposition is used instead of inheritance
    • CDuck class is too abstract
    • DInheritance is used instead of composition
  2. 2.A developer wants to add a new type of Duck that can fly with a jetpack. Which approach allows this without modifying existing Duck classes?

    • AOverride the fly method in the new Duck class
    • BImplement a new FlyBehavior and inject it
    • CModify the Duck class to include jetpack logic
    • DAdd a jetpack method to the Duck class
  3. 3.Why is using composition over inheritance beneficial in the Duck example?

    • AIt allows behavior to be changed at runtime
    • BIt simplifies the Duck class hierarchy
    • CIt makes all ducks fly
    • DIt avoids the need for interfaces
  4. 4.A MuteDuck needs to be created that neither flies nor quacks. How should this be implemented using composition?

    • AInject FlyNoWay and MuteQuack behaviors
    • BCreate a new subclass without fly() and quack()
    • COverride fly() and quack() with empty methods
    • DUse the Duck class without any behavior
  5. 5.What is a common issue with using inheritance in the Duck example?

    • ASubclasses may inherit unnecessary methods
    • BInheritance is slower than composition
    • CInheritance requires more memory
    • DAll ducks must fly
  6. 6.A developer argues that using inheritance is better because it reduces the number of classes. Why is this reasoning flawed?

    • AInheritance can lead to fragile base class problems
    • BInheritance is not always faster
    • CInheritance increases runtime errors
    • DComposition is more intuitive
  7. 7.In the Duck example, how can a ModelDuck be made to fly with a rocket at runtime?

    • AOverride its fly() method
    • BModify the Duck class to include rocket logic
    • CCreate a new subclass with rocket logic
    • DSet its FlyBehavior to FlyRocketPowered
  8. 8.What is the role of the QuackBehavior interface in the Duck example?

    • ATo make ducks fly
    • BTo simplify the Duck class
    • CTo allow different quacking behaviors to be injected
    • DTo enforce all ducks to quack