Hi friends,
Can you please help me with this question
class B {
private
String name;
public B(String name) {this.name = name;}
public String toString() {return name;}
protected void finalize() {System.out.print(name);}
}
class H {
static B ba = new B("Ba");
static int i = 1;
static B m1(B b) {return b = new B("B" + i++);}
public static void main (String[] args) {
B x = m1(ba); m1(x);
System.out.println(", " + ba + ", " + x);
}}
Which of the following could be a result of attempting to compile and run the program?
a. Ba, B1, B2
b. B1, Ba, B2
c. , Ba, B1
d. B2, Ba, B1
e. BaB1b2, null, null
f. B1B2, ba, null
Correct answer is C and D, i.e , Ba, B1 B2, Ba, B1
The author assumes that the Garbage collector runs after m1(x) returns, even if it runs why does the gc collect the object in the order B2, Ba, B1 ? AS far as i know order cannot be guaranteed. so after ,Ba, B1, any of the options a,b,d can occur...
Can you please reply if my understanding is correct.
You can find the question(Q9) here
http://www.danchisholm.net/oct1/mybook/chapter16/exam1.html
and the answers here
http://www.danchisholm.net/oct1/mybook/chapter16/exam1ans.html
Thanks and regards,
Srikanth