Exam 1z0-808 All QuestionsBrowse all questions from this exam
Question 120

Given the code fragments:

And:

Which statement is true?

    Correct Answer: C

    In the code provided, after line 11, only one object is eligible for garbage collection. Initially, three `Student` objects are created and referenced by `s1`, `s2`, and `s3`. The key lines to consider are 9, 10, and 11. After `s1 = s3;`, `s1` now refers to the same object as `s3`. Then, `s3 = s2;` makes `s3` refer to the same object as `s2`. Finally, `s2 = null;` removes the reference from `s2` to its object. Therefore, the original object referenced by `s1` (created at line 6) is no longer referenced by any variable and is eligible for garbage collection. The other objects referenced by `s3` and `s1` still have active references and are not eligible for garbage collection.

Discussion
HaridOption: C

after line 11, nobody referencing to s1, thus makes s1 gc eligible. Even though s2 is null, it is not eligible for gc, because s3 still pointing to it. S3 is not eligible for gc because s1 is pointing to it. Only object that nobody pointing to is S1. After line 11 only one object is eligible for GC = S1.

JongHwa

No. s1, s2, s3 are just variable that has Object's reference value(=address value). they are not Object. you said "nobody referencing to s1". that's incorrect. -> "Nobody is referencing the object that s1 initially had." is correct.

hemassridharOption: A

Correct, object created at line num 6 will be eligible for GC.

bshailesh3Option: B

Answer B is correct, as after assignment of null two references are still pointing. Tested.

krkpnr

and that means correct aswer is C.

brianhuang881215Option: C

obviously,ans is C s2 in its heap area is null, so it is eligible for garbage collection

yyfabcOption: B

tested, 2 objects are still referenced. public static void main(String[] args) { Object w1 = new Object(); Object w2 = new Object(); Object w3 = new Object(); w1 = w3; w3 = w2; w2 = null; System.out.println(w1); System.out.println(w2); System.out.println(w3); }

RAADEL3IMLAKOption: C

answer c is correct, tow objet is référenced

akbiyikOption: C

Answer is C

carlosworkOption: C

For those who want an explanation, follow one from stackoverflow: https://stackoverflow.com/questions/68313719/java-object-garbage-collection

iSnoverOption: A

The correct answer is the letter A, the reason is that the 3 variables are local variables, so their scope is inside the main method, after line 11 it leaves the main method and the 3 variables lose their usability because they leave their scope , so all 3 are eligible to enter the Garbage collector.

ludaka

Good point, but I think that your answer applies after line 12.

archer1903Option: C

Answer is C

biggeek123Option: A

the answer is A after line 11 all are unreferenced