my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on garbage collection from mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on garbage collection from mock exam" Watch "Question on garbage collection from mock exam" New topic
Author

Question on garbage collection from mock exam

Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
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
Antonio Tercero
Ranch Hand

Joined: Jun 05, 2008
Posts: 110
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


SCJP 5, SCWCD 5
Sanket Meghani
Greenhorn

Joined: Jan 15, 2007
Posts: 14
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

Joined: Feb 20, 2007
Posts: 88
i thought option c) looks good than d) . So i marked d).
thanks for your explanation
Saurabh Vyas
Ranch Hand

Joined: Sep 02, 2003
Posts: 72
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

Joined: Feb 20, 2007
Posts: 88
Yes Saurah you are correct.
i forgot it
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
This question is from my web site. My questions always come with answers and explanations.


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on garbage collection from mock exam
 
Similar Threads
OCP book (K & B) - Exam2 - Num 50 - GC question
Garbage Collection Question from Devaka's Exam Labs for SCJP6 - diagnostic exam question number 70.
Object for GC....
help on Garbage Collection analysis
Flow control