Page 1 of 1

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

Posted: Tue May 20, 2014 9:43 am
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:

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

Posted: Tue May 20, 2014 10:58 am
by admin
As explained in the explanation, it happens because there is no break after case 7.

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

Posted: Fri Nov 13, 2015 1:29 pm
by toolforger
Is the broken formatting intentional?

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

Posted: Fri Nov 13, 2015 8:59 pm
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.

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

Posted: Sat Nov 14, 2015 2:06 pm
by toolforger
Thanks, good to know.

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

Posted: Tue Jan 19, 2016 8:41 pm
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?

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

Posted: Tue Jan 19, 2016 9:35 pm
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.

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

Posted: Thu Jan 21, 2016 8:12 am
by Deleted User 2655
Returned 3.

Thanks Paul.

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

Posted: Wed Feb 03, 2016 2:07 pm
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?

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

Posted: Wed Feb 03, 2016 9:50 pm
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.

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

Posted: Mon Aug 29, 2016 2:35 pm
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 :)

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

Posted: Fri Dec 15, 2017 6:28 pm
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??

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

Posted: Fri Dec 15, 2017 9:20 pm
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.

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

Posted: Sun Dec 17, 2017 7:32 am
by Rinkesh
Okay thank you Paul.

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

Posted: Sun Jul 08, 2018 12:31 pm
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 : {}
        }

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

Posted: Sun May 05, 2019 9:04 am
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?

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

Posted: Sun May 05, 2019 11:15 pm
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.

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

Posted: Tue Jan 12, 2021 9:01 am
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.

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

Posted: Tue Jan 12, 2021 9:39 am
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.)

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

Posted: Tue Jan 12, 2021 11:32 am
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 :)