← 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?
- A.HashSet stores every element twice
- B.
size()was overridden incorrectly - C.HashSet's
addAllcalls the overriddenaddfor each element, so each name was counted twice - D.
List.ofcontains 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 →