wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock Exam Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock Exam Question" Watch "Mock Exam Question" New topic
Author

Mock Exam Question

Harry Singh
Ranch Hand

Joined: May 02, 2001
Posts: 124
Hi Java Gurus,
Just got this question in one of the mocks, and i got it wrong.



What will be the output of this code?
Regards,
Harjinder
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Harjinder
Output is

It's so because the return value in finally replaces the earlier return value. The general rule is the last return value will be held.
Arun Subbu
Greenhorn

Joined: Aug 30, 2003
Posts: 25
Hi Harjinder,
I am new to Java and am also planning to take the certification exam. But looking into the code you gave, i believe the code wont even compile.You have a private constructor.You can't instantiate the phr4jr class. If the code were to have a legitimate way to create the object, then the answer would be the one provided by the earlier reply by another person.
Java gurus correct me,if i had said anything stupid.(as i told i am just learning java and trying to find answers to as many questions as possible to take the certificatione xam.)
Christopher Brooks
Greenhorn

Joined: Sep 16, 2002
Posts: 16
Hi Arun,
You can have a private constructor. I can confirm that I copied and pasted the code into SunONE IDE and it compiled no problem and the output was indeed:
123
2
I must admit though I have never seen this double return thingy, I thought that would cause it to fail compilation. Anyone have some more detailed explanation of what happens when the first "return" is reached?


SCJP 1.2
Christopher Brooks
Greenhorn

Joined: Sep 16, 2002
Posts: 16
Oh Arun,
The private constructor means that you have to instantiate it from somewhere within its own class. You can't instantiate it from another class (well, except from an inner class).
Maybe its better to say - you can't instantiate it from outside its own class.
Arun Subbu
Greenhorn

Joined: Aug 30, 2003
Posts: 25
Hi Christoper,
Thanks for pointing out my mistake. I was also writing so many test programs, and i tried to instantiate the class with private ctor from a different class. my bad.when you feel that you are pretty much prepared to take the exam, you slip like this. . i am also using sun IDE, it did give me a warning .
Harry Singh
Ranch Hand

Joined: May 02, 2001
Posts: 124
Hi Pals,
Yes, we can have a private constructor but that essentially means that this class cant be instantiated from another class.
But about this question, i mean double Return valus. How does this code doesnt gives any compiler error? Wont the compiler yell as the first return statement never has any impact. So if the complier can tell sumthing like " unreachable statement" then why shouldnt compiler complains sumthing about this?
And how about the control in this program.After the first return statement, the control should return back.. isnt it? and if not then why... and if not then the whole purpose of return statement is defeated. isnt it?
Regards,
Harjinder
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
The finally clause return overrides the return that is inside the try - thats the whole idea of finally - to ensure that the code inside finally gets executed no matter what happens in the try.
(The only exception being a System.exit())
Bill
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
This is a good tricky question, and at the beginning, I mistakenly thought it wouldn't compile because no exception is possibly thrown in the try block. After compiling it with JDK 1.4.2, I got a warning:

The output is:
123
2
Did anybody else get a warning?
Can anybody explain what is the exact case for compilation failure when no exception is thrown in try?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Vad
Proabably this is what you are looking for.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Thanks Anupam, I followed your link. catch(Exception e){} should also work if you substitute Throwable.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Vad Fogel:
After compiling it with JDK 1.4.2, I got a warning:

The output is:
123
2
Did anybody else get a warning?

Hi Vad,
You may want to follow this
link if you're interested in what is causing the warning message.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Hi Alton, thanks for the link. So, finally, is it a bug or an expected behaviour? And can we always rely on that feature where return statement in try block is ignored for the sake of finally's return?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock Exam Question
 
Similar Threads
examlab mockup 1 question
Overriden method
return triggers method end but overriden?
Doubt in initialization block.