Skip to content
Sharp

Core Java · interview question

What happens if two objects in a HashSet have different hashCodes but are considered equal by the equals method?

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

Correct answer

D. Both objects can be stored in the HashSet.

Why

If two objects have different hashCodes but are considered equal by the equals method, both objects can still be stored in the HashSet. The HashSet uses hashCode to determine the initial bucket but relies on equals to check for equality within a bucket.

Test yourself on Core Java

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

Start free test
All Core Java questions →