Skip to content
Sharp

Core Java · interview question

Why does overriding hashCode but not equals in a Java class change runtime behavior?

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

Correct answer

B. The class might break the contract of hash-based collections.

Why

Overriding hashCode without equals can lead to inconsistencies where logically equal objects are treated differently by collections. This breaks the general contract, potentially causing logical errors.

Test yourself on Core Java

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

Start free test
All Core Java questions →