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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
pmrraghav
Posts: 2
Joined: Thu May 18, 2017 4:10 am
Contact:

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

Post 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 ;
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

pmrraghav
Posts: 2
Joined: Thu May 18, 2017 4:10 am
Contact:

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

Post by pmrraghav »

Thank you.

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post 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!!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
If you like our products and services, please help us by posting your review here.

Lunarkiran
Posts: 4
Joined: Mon Feb 26, 2018 3:41 am
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 41 guests