Multiple choice technology programming languages

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”?

  1. itemMap.remove("orange"); itemMap.put("orange", new Item("Orange") );

  2. ((Item)itemMap.get(“orange”)).name = “Orange”;

  3. itemMap.put("orange", new Item("Orange") );

  4. None of above

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Option B is simplest and correct because it retrieves the existing Item object for key "orange" and modifies its name attribute in-place. This is more efficient than removing and re-adding the entry. Option A unnecessarily removes the key-value pair and creates a new Item object, which is redundant when you can just modify the existing object's attribute. Option C would replace the entire value object with a new one rather than modifying the existing one.