• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on garbage collection from mock exam

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


source : http://www.jchq.net/

At which point will the word "Harpic" be printed out when this code is executed?


class Harpic
{
public void finalize()
{
System.out.println("Harpic");
}
}
public class ArmitageShanks {
Harpic har;
public static void main(String...strings)
{
ArmitageShanks ar = new ArmitageShanks();

}
public void oui()
{
har = new Harpic();
mno(har);
//one
har = null;

}
public void mno(Harpic har)
{
Harpic pic = har;
//two
pic = null;
//three
har = null;
}
}

a)After executing the line after the comment //one
b)After executing the line after the comment //two
c)After executing the line after the comment //three
d)It is impossible to say, String "Harpic" may not be output at all


For the above i marked c) as the answer. But they have given d) .
can any one explain it?
Is this type of questions come in real exam?

rami
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't predict when the GC will run.
Only the JVM decides when to run the GC, you can only suggest it.
The garbage collector makes no guarantees, finalize() may never run
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
D is the correct answer.

Java do not have a concept of destructors as in C++. They provide an alternative solution to clean up our resources through finalize() method.

The finalize() method will be called when the garbage collection routine runs and object is garbage collected. As you know, there is no guarantee either when the GC will run or whether it will run at all or not.

So if the GC runs and chooses the object for garbage collection during execution of your programme, then finalize() method will be called and "Harpic" will be printed on standard output. If JVM chooses not to run GC during execution of your programme, then finalize() method will not be called at all and "Harpic" will never gets printed.

So it is not deterministic to say when "Harpic" will be printed or it may not be printed at all.


Regards,
Sanket
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i thought option c) looks good than d) . So i marked d).
thanks for your explanation
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If we observe the question closely, we can make out that an instance of Harpic class actually never gets created.


public static void main(String...strings)
{
ArmitageShanks ar = new ArmitageShanks();

}



Thus there is no question of it getting garbage collected.

I guess in original question a line is missed while posting the question. Thus main method should look something like below :


public static void main(String...strings)
{
ArmitageShanks ar = new ArmitageShanks();
ar.oui();
}

 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Saurah you are correct.
i forgot it
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from my web site. My questions always come with answers and explanations.
 
Marcus Green
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The text that Ram has pasted in is not the text of the question. Here is the actual text

class Harpic{
public void finalize(){
System.out.println("Harpic");
}

}

public class ArmitageShanks{
Harpic har;
public static void main(String argv[]){
ArmitageShanks as = new ArmitageShanks();
as.oui();
}
public void oui(){
har = new Harpic();
mno(har);
//one
har=null;

}
public void mno(Harpic har){
Harpic pic=har;
//two
pic=null;
//three
har=null;
}
}

And here is the answer/explanation

The Correct Answer is
4) It is impossible to say, the String "Harpic" may not be output at all.

The instance of the Harpic class will probably not be garbage collected at all, and thus the finalize method will not be run (but it could be). It is possible that the application will finish before the garbage collecting thread address that Object.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic