We have 3 Student objects initially.
first is reachable by S1
Second is reachable by S2
Third is reachable by S3
When we either nullify the reference or reassign it, we make it unreachable.
There are generally four ways to make an object eligible for garbage collection.
https://www.geeksforgeeks.org/garbage-collection-java/
Nullifying the reference variable
Re-assigning the reference variable
An object created inside the method
Island of Isolation
We are in the reassignment
S1 = S3, we just made the object that had reference variable of S1 unreachable
S3 = S2, we just made the object that had reference variable of S3 unreachable
S1 = S2, we just made the object that reference variable of S1 unreachable. This object was the same as above.
So in total, we have 2 objects eligible for garbage collection.
3 objects are created and at the end only S2 is being referenced other S1 and S2 has no reference. So answer is 2 objets are eligibe for garbage collection
The correct answer is letter A, an object is eligible for the garbage collector when it is no longer being used, as after line 11 no object is being used, the 3 are elected for the garbage collector. Even though "s2" can still be accessed, then on line 11 its scope is over so it is also elected to the garbage collector.
upvoted 3 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
UAK94
Highly Voted 2 years, 3 months agoMPignaProTech
Most Recent 2 months, 3 weeks agoBelloMio
7 months, 1 week agoBelloMio
7 months, 1 week agoShaileshRaj
1 year, 3 months agoRAADEL3IMLAK
1 year, 11 months agoakbiyik
2 years, 1 month agocarloswork
2 years, 1 month agoiSnover
2 years, 3 months ago