So I've recently been to job interview and was asked the following question. (Actually it was just a test writing, so I couldn't ask any questions)
At the end of the main method, how many objects will be eligible for garbage collection?
public class Main {
public static void main(String[] args) {
Object obj;
for (int i = 0; i < 5; i++) {
obj = new Object();
}
obj = null;
}
}
(A) 0
(B) 1
(C) 5
I know it's 0 because at least one object (obj) will be garbage collected, but I also know that obj is not really the object, it's just a reference to it. So my answer was 5.
Is that correct? If not, then why?
Five. but it must me mentioned at which line too?? - Vikrant Kashyap