🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Composition vs Inheritance: all 10 questions

Quiz: Composition vs Inheritance · Question 1 of 10

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

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

  1. A.HashSet stores every element twice
  2. B.size() was overridden incorrectly
  3. C.HashSet's addAll calls the overridden add for each element, so each name was counted twice
  4. D.List.of contains duplicates
Answer & explanation

Correct answer: HashSet's addAll calls the overridden add for each element, so each name was counted twice

The subclass counted 3 in addAll, and the 3 internal add calls counted 3 more.

Can you get the rest right without peeking?

Take the timed Quiz: Composition vs Inheritance quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →