Srikanth Manvi

+ Follow
since Feb 21, 2009
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Srikanth Manvi

Hi friends,

Can you please help me with this question

class B {
private String name;
public B(String 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
and the answers here

Thanks and regards,