Skills101
← All practice quizzes

Quiz: Composition vs Inheritance

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

Why "favour composition over inheritance" is real advice and not a slogan: a runnable Java bug caused by inheritance, the same feature built with composition, and a checklist for choosing.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    Why did the inheritance-based CountingSet report 6 after adding 3 names?

  2. Question 2

    The problem where a subclass breaks because of hidden internal details of its parent is called:

  3. Question 3

    In the composition version, why is there no double counting?

  4. Question 4

    What does this print?

    class Base {
        void hello() { System.out.print("Base "); greet(); }
        void greet() { System.out.print("hi"); }
    }
    
    class Child extends Base {
        @Override
        void greet() { System.out.print("hello"); }
    }
    
    public class Main {
        public static void main(String[] args) {
            new Child().hello();
            System.out.println();
        }
    }
    
  5. Question 5

    A game character needs to fly AND swim, and abilities can change during the game. Which design fits best?

🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →