--------------------<BR>Java will never die.<P>Kenneth
sona<br />SCJP
--------------------<BR>Java will never die.<P>Kenneth
Q18 Which is the earliest line in the following code after which the object created on line marked (0) will be a candidate for being garbage collected, assuming no compiler optimization are done ?
<code>
public class Q769a9 {
static String f() {
String a = "hello";
String b = "bye"; // (0)
String c = b + "!"; // (1)
String d = b
b = a; // (2)
d = a; // (3)
return c; // (4)
}
public static void main(String args[]) {
String msg = f();
System.out.println(msg); // (5)
}
}
</code>
Select the one right answer.
<code>
(a) The line marked (1)
(b) The line marked (2)
(c) The line marked (3)
(d) The line marked (4)
(e) The line marked (5)
</code>
Answer provided at pp 723 is :
<code>
Q 18 (c)
At (1), a new string is constructed using the "bue" string, but
no additional reference to "bye" string object are created. On the line below. an additional refernce to "bye" is created. At line (2) , the original refernce to "bye" in b is overwritten,
but d still contains refernce to "bye". At line (3), the reference d also stops denoting "bye". Now the "bye"
string object cannot be referenced through any reference, and thus is a candidate for garbage collecting.
</code>
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |