• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

garbage collection

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can someon explain the answer to the problem ? I understood why we get a but c,d,e I am confused .........

class A {
private String name;
private A otherA;
public A(String name) {this.name = name;}
public void other(A otherA) {this.otherA = otherA;}
public A other() {return otherA;}
public String toString() {return name;}
protected void finalize() {System.out.print(name);}
}
class B {
public static void m1() {
A a1 = new A("A1"), a2 = new A("A2"), a3 = new A("A3"), a0 = a3;
a1.other(a2); a2.other(a3); a3.other(a1);
for(int i = 0; i<4; i++){System.out.print(a0 = a0.other());}
}
public static void main(String[] args) {m1(); System.gc();}
}

Which of the following could be a result of attempting to compile and run the program?
a. A1A2A3A1
b. A0A0A0A0A1A2A3
c. A1A2A3A1A2A3
d. A1A2A3A1A1A2A3
e. A1A2A3A1A3A2A1
f. A0A1A2A3A1A2A3

Answers a,c,d,e

Thank you
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recall that an object is eligible for garbage collection when it can't be reached from an active thread.

Once method m1() has returned to method main(), local reference variables a0, a1, a2, and a3 are gone. While the objects refer to each other, they can't be reached from main(). Since garbage collection of each eligible object may or may not happen, all four possible answers are correct.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic