Skip to content
Sharp

Core Java · interview question

Which method should be overridden to ensure correct behavior of keys in a HashMap?

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

Correct answer

D. Both equals and hashCode

Why

In HashMap, keys are stored and retrieved based on their hashCode, and equality is checked using equals. Both methods must be overridden to maintain the map’s integrity.

Test yourself on Core Java

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

Start free test
All Core Java questions →