About Question enthuware.ocajp.i.v7.2.1148 :

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

Moderator: admin

Post Reply
Nisim123
Posts: 42
Joined: Mon Jan 20, 2014 2:26 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1148 :

Post by Nisim123 »

The last part of the explanation says:
Now, in the switch there is no break statement.
So both - case 7: amount = amount * 2;
and case 6: amount = amount + amount; are executed.
so the final amount becomes 400.
I still don't understand why case 6 is executed while there is no place in the code that returns this value ah... OK now after a recheck i concluded that it is probably case 3:
a small editor mistake... :ugeek:

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

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by admin »

As explained in the explanation, it happens because there is no break after case 7.
If you like our products and services, please help us by posting your review here.

toolforger
Posts: 38
Joined: Fri Nov 13, 2015 4:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by toolforger »

Is the broken formatting intentional?

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

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by admin »

It doesn't look like intentional in this case but in the exam you will find code snippets with intentional as well as unintentional broken formatting.
If you like our products and services, please help us by posting your review here.

toolforger
Posts: 38
Joined: Fri Nov 13, 2015 4:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by toolforger »

Thanks, good to know.

Deleted User 2655

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by Deleted User 2655 »

"Now, as finally has return 7;, this value supersedes 3."

Why does 7 supersede 3 in this case,
  • Is it because 7 is larger than 3?
or
  • Because values returned in finally block take precedence over the catch block?
For example given same example;

Code: Select all

     catch(Exception e)
            { return 7; }             
     finally
            {return 3; }          
Would value in catch block be returned or value in finally block?

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

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by admin »

sugarkanke wrote:"Now, as finally has return 7;, this value supersedes 3."

Why does 7 supersede 3 in this case,
  • Is it because 7 is larger than 3?
or
  • Because values returned in finally block take precedence over the catch block?
Because values returned in finally block take precedence over the catch block.
For example given same example;

Code: Select all

     catch(Exception e)
            { return 7; }             
     finally
            {return 3; }          
Would value in catch block be returned or value in finally block?
What happened when you tried it out?
-Paul.
If you like our products and services, please help us by posting your review here.

Deleted User 2655

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by Deleted User 2655 »

Returned 3.

Thanks Paul.

Ado_14
Posts: 1
Joined: Wed Feb 03, 2016 1:59 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by Ado_14 »

"Because values returned in finally block take precedence over the catch block."


For example given same example;

Code: Select all

     catch(Exception e)
            { return 7; }             
     finally
            {return 3; }          
Would value in catch block be returned or value in finally block?

In an answer it is written:"As there is a finally associated with the try/catch block, it is executed before anything is returned" (I assume) in catch block.

But I wonder then why the code in catch block compiles? If it is unreachable code, it should not compile or should it?

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

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by admin »

Just because there is a finally block, that doesn't mean the catch block is unreachable. finally block is executed after catch is executed (if the exception is caught by that catch block). It is only the return value of the catch or try block that is superseded by the value returned by the finally block.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

soncrash
Posts: 8
Joined: Sat Aug 27, 2016 2:51 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by soncrash »

Java is weird... I also typed for unreachable block - it wont compile...
Also when I pasted this code into Eclipse it warns me that "finally block does not complete normally".

Nice question however :)

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by Rinkesh »

Suppose that seed=11 so I will get 1 instead of 7 which is returned in finally because finally is not executed compulsorily unless and until we execute the code in try block or if there is a System.exit(),Am I Right??

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

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by admin »

finally isn't executed if there is a System.exit is executed in the try block or catch block otherwise. Except that, a finally block is always executed.
If you like our products and services, please help us by posting your review here.

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by Rinkesh »

Okay thank you Paul.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by __JJ__ »

Funny I had thought that having just "default:" would be a syntax error but clearly it isn't. All the following are legal:

Code: Select all

        switch( 3 ){
           default : 
        }
        switch( 4 ){
           case 5 :
        }
        switch( 3 ){
           default : ;
        }
        switch( 3 ){
           default : {}
        }

Solid15
Posts: 1
Joined: Sun May 05, 2019 8:43 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by Solid15 »

Yesterday I had a mock question saying it doesn't compile of you return in both the try, catch and final block. (I think it was in mock exam 3, otherwise it was in mock exam 5). I believe it had to do with unreachable code. I answered this question wrong because I thought it would return the finally return. Now I see this question and I answer "does not compile" and it is wrong, because apparently the finally return can overwrite the previous return.

I'm confused. Doesn't the compiler know the finally return is unreachable because of the if-else clause within the try-block, as opposed to the previous example (if I remember the details of the previous question correctly).

Some testing in my IDE indicates that it compiles. So either the question from the previous mock test was wrong, or something else is involved there that I don't understand.

QUESTION: is there a way to make try{return}catch{return}finally{return} not compile? If so, how?

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

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by admin »

I couldn't find any explanation text that says anything like, "it doesn't compile of you return in both the try, catch and final block.".

finally block is always executed, so, a return statement in finally cannot be unreachable just because it is present in the finally block or because there is a return statement or an if/else statement in the try or catch blocks.

The rules of unreachable code are same for all types of blocks. So, a simple code like this:

Code: Select all

try{
   return;
}catch(Exception e){
   return;
}catch(RuntimeException e){ //this catch block is unreachable
   return;
}     
finally{
    if(true ) return ;
    else return;
    return; //this is unreachable
}
will not compile.

If you can find the question that you think has a problem, let us know.
If you like our products and services, please help us by posting your review here.

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by Denyo1986 »

Solid15 wrote:
Sun May 05, 2019 9:04 am
Yesterday I had a mock question saying it doesn't compile of you return in both the try, catch and final block. (I think it was in mock exam 3, otherwise it was in mock exam 5). I believe it had to do with unreachable code. I answered this question wrong because I thought it would return the finally return. Now I see this question and I answer "does not compile" and it is wrong, because apparently the finally return can overwrite the previous return.

I'm confused. Doesn't the compiler know the finally return is unreachable because of the if-else clause within the try-block, as opposed to the previous example (if I remember the details of the previous question correctly).

Some testing in my IDE indicates that it compiles. So either the question from the previous mock test was wrong, or something else is involved there that I don't understand.

QUESTION: is there a way to make try{return}catch{return}finally{return} not compile? If so, how?
I am with you. Thought of the same question, also opted for unreachable code and failed. Will supply the question if I find it again.

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

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by admin »

You might want to check out questions 2.984, 2.1200, and 2.1323. They touch upon reachable statement issue but are different.
(In ETS Viewer s/w, you can do Control + Q and then enter 2.984 to open that question.)
If you like our products and services, please help us by posting your review here.

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1148 :

Post by Denyo1986 »

Great. Supplying those numbers was super-helpful. Thanks admin.

The question I had in mind was 2.1200. I looked at it again and found the difference. In that question there is another return statement after the try/catc/finally block and that causes all the issues. So its not really comparable to this situation (but seemed so from my memory).

Thanks for being such a great support here...really good exam prep experience :)

Post Reply

Who is online

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