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

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

Moderator: admin

Post Reply
Rafael
Posts: 3
Joined: Thu Jul 11, 2013 5:39 am
Contact:

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

Post by Rafael »

What will the following program print?

class Test{    
public static void main(String args[]){       
int k = 9, s = 5;       
switch(k){          
default :          
if( k == 10) { s = s*2; }
         else{             s = s+4;
            break;          }         
 case 7 : s = s+3;       
}       
System.out.println(s);    
}
}
Possible answers: 5 - 9 - 12 - It won't compile.

The correct answer is 9, but I marked 12 because I thought keyword break finished 'else' block but not 'default' block. So, if I will write for example:

default:
if(true){
if(true){
if(true){
if(true){
s=0;
break; <- do this Break finish default case too?
}}}}
s=12345; <- is this Dead code?

Thank you so much¡ ;)

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

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

Post by admin »

1. Yes.
2. No, if(true) is a a special case that doesn't cause unreachable/dead code anywhere.

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

st.lisker
Posts: 22
Joined: Sat Jun 30, 2018 6:11 am
Contact:

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

Post by st.lisker »

Hey! It's no break statement can be compiled in the else block!.. It must be fail to compile.

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

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

Post by admin »

What happened when you actually tried to compile the given code? Observe that the break is inside the switch block.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 60 guests