aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Flow control Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Flow control" Watch "Flow control" New topic
Author

Flow control

deshdeep divakar
Ranch Hand

Joined: Apr 19, 2004
Posts: 91
At which point will the word "Harpic" be printed out when this code is executied?
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;
}
}



1) After executing the line after the comment //one
2) After executing the line after the comment //two
3) After executing the line after the comment //three
4) It is impossible to say, the String "Harpic" may not be output at all.

ds is a question from Marcus green,culd any buddy would help me to trace the flow thanks


"Do not be afraid of going slow, be afraid of standing still"
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
U cannot say when garbage collector runs and finalize method is called.
So, according to me correct option would be "4"
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
4


Tony Morris
Java Q&A (FAQ, Trivia)
sabeer
Ranch Hand

Joined: Mar 15, 2005
Posts: 41
In the given code the method "finalize" of class "Harpic" never called from the main method. So choice 4 is correct answer.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

In the given code the method "finalize" of class "Harpic" never called from the main method. So choice 4 is correct answer.


4 is the correct answer, but not for this reason.
An object will certainly have the finalize() method called when it is collected. An object is not guaranteed to be collected, not ever. Therefore, the finalize() method is not guaranteed to be called. Therefore, 4 is the correct answer.
subramanian annamalai
Greenhorn

Joined: Mar 06, 2005
Posts: 22
i do would like to know let as assume gc is getting called , in that case what will be thr output;
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by subramanian annamalai:
i do would like to know let as assume gc is getting called , in that case what will be thr output;



If we assume that after having no reference that object will be collected immediately , then answer would be 1 .

hope I am right .
[ March 20, 2005: Message edited by: rathi ji ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Flow control