🎴 Flashcard Mode

Java Programming Fundamentals

Card1 / 20
Mastered0
Review0
QuestionClick to flip

Which is simplest and correct code snippet needs to be put in the main function to replace the value object of the HashMap for the key “orange”?

AnswerClick to flip back
A
((Item)itemMap.get(“orange”)).name = “Orange”;
💡 Explanation:

Option B directly mutates the Item object inside the HashMap by accessing it via get() and setting its name field. This changes the value object's content without replacing the reference. Option C creates a new Item but doesn't prevent other references to the old one.

Change Mode