Skip to content
Sharp

Collections · interview question

What does the replace() method do in a HashMap when the key exists?

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

Correct answer

B. Updates the value for the key and returns the old value.

Why

The replace() method updates the value associated with a key if it exists and returns the old value. If the key does not exist, it does nothing.

Test yourself on Collections

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

Start free test
All Collections questions →