Skip to content
Sharp

Core Java · interview question

How might using a mutable object as a key in a HashMap lead to subtle bugs?

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

Correct answer

C. The mutable key can be changed, making it irretrievable.

Why

When a mutable key's state changes, its hashCode may change, making the key irretrievable. This breaks the contract that the key's hashCode and equals relationship should remain consistent during its lifetime.

Test yourself on Core Java

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

Start free test
All Core Java questions →