• 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

Mock Exam Question

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Christopher Brooks
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vad
Proabably this is what you are looking for.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anupam, I followed your link. catch(Exception e){} should also work if you substitute Throwable.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic