Question 17
class B {
private String name;
public B(String s) {name = s;}
public void finalize() {System.out.print(name);}
}
class E {
public static void m() {
B x1 = new B("X"), y1 = new B("Y");
}
public static void main(String[] args) {
m(); System.gc();
}}
Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?
a. Prints: XY
b. Prints: YX
c. Prints: XXYY
d. Prints: YYXX
e. Nothing is printed.
f. There is no guarantee that the garbage collector will finalize any objects that are eligible for garbage collection.
The correct answers are a, b, e, f. I selected only f... and that turned out wrong. My understanding is option f is not a
result of compiling and running the program (so I got confused in the first place), but rather a guaranteed behaviour statement, so it should've only been present had the question asked about
precise output There are two good questions in this one struggling to get out.
[ October 16, 2003: Message edited by: Vad Fogel ]