Given the code fragments:
And:
Which statement is true?
Given the code fragments:
And:
Which statement is true?
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.
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.
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.
Correct, object created at line num 6 will be eligible for GC.
Answer B is correct, as after assignment of null two references are still pointing. Tested.
and that means correct aswer is C.
obviously,ans is C s2 in its heap area is null, so it is eligible for garbage collection
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); }
answer c is correct, tow objet is référenced
Answer is C
For those who want an explanation, follow one from stackoverflow: https://stackoverflow.com/questions/68313719/java-object-garbage-collection
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.
Good point, but I think that your answer applies after line 12.
Answer is C
the answer is A after line 11 all are unreferenced