Mastering Java 2025 – 400 Free Practice Questions to Pass the 'Thinking in Java' Exam

Question: 1 / 510

What does Map.put(key, value) do?

Adds a key if it doesn't exist

Associates the specified value with the specified key

Map.put(key, value) does not add a key if it doesn't exist, contrary to option A. Instead, it associates the specified value with the specified key.

Option C is incorrect because Map.put(key, value) does not ensure that the map contains the specified key-value pair, it actually adds or replaces the value for the given key.

Similarly, option D is incorrect because Map.put(key, value) does not replace the value for the specified key if it exists. Instead, it adds or replaces the value for the specified key without checking if it already exists. This means if the same key is put multiple times, the latest value will be associated with the key.

Get further explanation with Examzify DeepDiveBeta

Ensures the map contains the specified key-value pair

Replaces the value for the specified key if it exists

Next Question

Report this question

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy