At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.
void method X() {
String r = new String("abc");
String s = new String("abc");
r = r+1; //1
r = null; //2
s = s + r; //3
} //4
a) Before statement labeled 1
b) Before statement labeled 2
c) Before statement labeled 3
d) Before statement labeled 4
e) Never.
the ans says its d
but even if an object is created inside a method it is allocated space on the heap
so even if the method exits the object ref by out lives int the memory then how come it will be coolected before line 4
even if it is collected for gc after the method invocation it will be collected after line 4 because there actully the method dies
please claer this
QUESTION 2)
How can you ensure that the memory allocated by an object is freed. Select the one correct answer.
a) By invoking the free method on the object.
b) By calling system.gc() method.
c) By setting all references to the object to new values (say null).
d) Garbage collection cannot be forced. The programmer cannot force the compiler to free the memory used by an object.
the ans says d
but we can ensure that the object is gc ed by killing all its live rferences though not 100% sure about it
the ans d is the behavoiur of the gc in
java i think the question is wrongly worded
please correct me