The answer should be choice b, 1.
First off since "name" is not explicitly initialized to null I will assume these are class level variables that are implicitly initialized ;-)
Now, a new object is created when newName is assigned "Nick" but it is immediately dereferenced since newName is assigned to another string object "Jason". The "Nick" object is eligible for GC since it is in a dereferenced state. name is assigned to new String object with value "Frieda" but the reference is transferred to newestName so it will not be dereferenced when name is assigned null.. so "Frieda" is not eligible for GC since it can still be used using the newestName reference ;-)
Kevin
Originally posted by HS Singh:
How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
a) 0
b) 1
c) 2
d) 3
e) 4
Can someone take a look at this. Thanks