Skip to content
Sharp

Core Java · interview question

When designing an in-memory catalog using HashMap, why is it risky to use mutable objects as keys?

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

Correct answer

C. Because the hashCode might change, causing inconsistent retrieval.

Why

Using mutable objects as keys in a HashMap is risky because if the mutable fields change, the hashCode may change. This means the object might not be found in the map under its new state, leading to inconsistent retrieval and bugs in the application logic.

Test yourself on Core Java

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

Start free test
All Core Java questions →