• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Exam on Monday -- pl help

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public float parseFloat( String s )
{
float f = 0.0f; // 1
try
{
f = Float.valueOf( s ).floatValue(); // 2
return f ; // 3
}
catch(NumberFormatException nfe)
{
f = Float.NaN ; // 4
return f; // 5
}
finally {
return f; // 6
}
return f ; // 7
}
options:
Remove line 3,6
Remove line 3,7
Remove line 7
Remove line 5,6
prob: All these options are corrr.
But anyshorts to know which one would be right. Becuse in the exam we wouldn't have time to check and code each one of them.
Thanks padmini
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't say that I understod your question correctly. Is there a question involved.
If the question is: which line can be removed and still get the same reult, then yes all of those examples are correct.
Explained:
1. both 3 and 6 can be removed since there is a return at the end of the method. Since all returns are alike, only the one at the end is needed.
2. both 3 and 7. Since this leaves the return in the finally-block, a return will always be made. Actually you could remove line 5 too.
3. Line 7. Since The finally-block contains a return, this line will never be reached anyway.
4. 5 and 6 Given that there is a return at the end of the method, the returns leading up to it could be removed.
To be blunt, all you need is a return at lone 6 OR a return at line 7 OR a return at both line 3 & 5.
/Mike
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recognize this question. The code won't compile because line 7 is unreachable, and the question is "removing which line(s) will allow this code to compile?" (or something like that).
The basic problem is that either line 6 or line 7 needs to be removed. If you have a return statement in a finally block, then any code following the finally block will never be executed, and is therefore unreachable, prompting a complaint from the compiler. Removing line 7 alone will resolve the problem.
You can, alternately, remove line 6. However, you would also then need to remove either line 3 or line 5 -- otherwise line 7 will remain unreachable because either line 3 or 5 will be executed 100% of the time.
There is no shortcut here, you just need to understand how return statements in try-catch-finally blocks may affect code following the block.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic