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; ...
-
Array Object
-
Neither Object
-
String Objec
-
Both of the Objects
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.