Skip to content
Sharp

Collections · interview question

You have a HashSet storing custom Product objects. Why might duplicate Products be inadvertently added?

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

Correct answer

B. Because the equals() and hashCode() methods are not overridden

Why

For a HashSet to correctly enforce uniqueness of custom objects, the equals() and hashCode() methods must be correctly overridden in the Product class to ensure proper comparison.

Test yourself on Collections

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

Start free test
All Collections questions →