Page 1 of 1

About Question enthuware.ocajp.i.v8.2.984 :

Posted: Thu May 18, 2017 4:15 am
by pmrraghav
Please help me on these points..

1. Where (i.e at which part of code) we can have return statement.

2. How to check the correct return value using loops, try-catch-finally blocks for a sample code like below?

public float parseFloat(String s){
float f = 0.0f;
try{
f = Float.valueOf(s).floatValue();
return f ;
}
catch(NumberFormatException nfe){
System.out.println("Invalid input " + s);
f = Float.NaN ;
return f;
}
finally { System.out.println("finally"); }
return f ;
}

Re: About Question enthuware.ocajp.i.v8.2.984 :

Posted: Thu May 18, 2017 4:35 am
by admin
You can have a return statement anywhere. There is no particular rule or restriction. But you have to keep in mind other rules such as you cannot have unreachable statements. It is not possible to discuss all this in a forum post, but if you are following any good Java programming book, you will get to know all such nitty-gritties as you go through it.

To find out a return value of a method, you need to execute the code mentally, line by line. That is the only way.

If you are a Java beginner, I will suggest you to first go through a good book and write small test programs before attempting mock questions.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v8.2.984 :

Posted: Fri May 19, 2017 1:38 am
by pmrraghav
Thank you.

Re: About Question enthuware.ocajp.i.v8.2.984 :

Posted: Mon Aug 14, 2017 5:54 am
by Sergey
Hello.
Why does "return f;" in fynally block is unreachable?
As the exception is already handled, control goes to finally which prints "finally" and then the try/catch/finally ends and "return f;" is executed. No?

Re: About Question enthuware.ocajp.i.v8.2.984 :

Posted: Mon Aug 14, 2017 9:26 pm
by admin
There is a return statement in catch block as well. So the method returns from catch itself. Since there is a finally block as well, the JVM will execute the finally block before returning the value returned by the return statement in catch block to the caller of the method.

Re: About Question enthuware.ocajp.i.v8.2.984 :

Posted: Mon Oct 23, 2017 9:10 am
by Javier
Hi!

If we comment out the last return f, and if we input a correct afloat number, the order of execution is?:
1. Print finally
2. Return the float number

Because if were the opposite, the return statement take the control back of the execution to the calling method and Finally block ("always is executed") couldnĀ“t be executed.

Is the return statement in suspense until the Finally block finish?

Thank you very much!!

Re: About Question enthuware.ocajp.i.v8.2.984 :

Posted: Mon Oct 23, 2017 9:13 am
by admin
Javier wrote:
Is the return statement in suspense until the Finally block finish?

Thank you very much!!
Yes. The exact sequence is explained in detail here: https://docs.oracle.com/javase/specs/jl ... ls-14.20.2

Re: About Question enthuware.ocajp.i.v8.2.984 :

Posted: Sun Apr 29, 2018 1:36 am
by Lunarkiran
first of all, there is no return statement in finally block and if catch block has one then also we can include return in finally and finally, finally's return will be sent not catch's.