Skip to content
Sharp

Collections · interview question

Why might a HashSet not prevent duplicates as expected when using custom objects?

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

Correct answer

C. HashCode() is overridden but equals() is not.

Why

If hashCode() is overridden but equals() is not, or vice versa, the HashSet may not correctly identify duplicates. Both methods need to be properly implemented to ensure the set's uniqueness contract is maintained.

Test yourself on Collections

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

Start free test
All Collections questions →