Skip to content
Sharp

Collections · interview question

What will be the output when attempting to add a duplicate element to a HashSet?

Multiple choiceMediumchapter-4-curatedpremiumcollectionsset-interface-and-hashset
Choose one answer
Answer and explanation

Correct answer

B. The duplicate element will be ignored.

Why

HashSet does not allow duplicate elements. When an attempt is made to add a duplicate, it is simply ignored, and the set remains unchanged.

Test yourself on Collections

A free test with instant, explained results. No account needed.

Start free test
All Collections questions →