Multiple choice technology programming languages

Which of the given options code is correct and will not waste memory. Note :- Defination of the Class Item is class Item { public int code; public String name; public Item(String name) { this.name = name; } } itemList is ArrayList containing 50 objects of Class Items.

  1. Item im = new Item(null); im = (Item ) itemList.get(0);

  2. Item im = new Item(“Done”); im = (Item ) itemList.get(0);

  3. Item im = null; im = (Item ) itemList.get(0);

  4. All of the above.

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

Option C is correct because it avoids unnecessary object creation. Options A and B both create new Item objects ('new Item(null)' and 'new Item("Done")') before immediately overwriting the reference with the retrieved element from itemList. This wastes memory as the newly created objects become unreachable and garbage collectible. Option C simply declares a null reference and then assigns the retrieved object - no wasted allocation.