Skip to content
Sharp

Collections · interview question

In a HashMap, what happens if you put() a new value for an existing key?

Multiple choiceMediumchapter-4-curatedpremiumcollectionsmap-interface-and-hashmap
Choose one answer
Answer and explanation

Correct answer

D. The old value is replaced by the new value.

Why

When you put() a new value for an existing key in a HashMap, the old value is replaced by the new value. The HashMap is updated to reflect the new value associated with that key.

Test yourself on Collections

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

Start free test
All Collections questions →