Skip to content
Sharp

Core Java · interview question

What happens if you override only the equals method and not hashCode in a Java class used in a HashSet?

Multiple choiceEasychapter-4-curatedpremiumcore-javaequals-and-hashcode-in-collections
Choose one answer
Answer and explanation

Correct answer

D. HashSet may contain duplicates.

Why

HashSet uses both equals and hashCode to determine object uniqueness. Without a consistent hashCode, objects that are considered equal by equals may end up in different buckets, leading to duplicates.

Test yourself on Core Java

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

Start free test
All Core Java questions →