Multiple choice technology programming languages

The following code creates one array and one string object. which object is eligible for garbage collection after the code executes? ... String[] students = new String[10]; String studentName = "Peter Parker"; students[0] = studentName; studentName = null; ...

  1. Array Object

  2. Neither Object

  3. String Objec

  4. Both of the Objects

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

Tracing the code: after creating the array and String, assigning 'students[0] = studentName' creates a second reference to 'Peter Parker'. Setting 'studentName = null' only removes one reference. The array object is still referenced by 'students' variable, and the String object is still referenced by 'students[0]'. Neither object loses all references, so neither is eligible for garbage collection.