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¡
About Question enthuware.ocajp.i.v7.2.1108 :
Moderator: admin
-
- Posts: 3
- Joined: Thu Jul 11, 2013 5:39 am
- Contact:
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1108 :
1. Yes.
2. No, if(true) is a a special case that doesn't cause unreachable/dead code anywhere.
HTH,
Paul.
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.
-
- Posts: 22
- Joined: Sat Jun 30, 2018 6:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1108 :
Hey! It's no break statement can be compiled in the else block!.. It must be fail to compile.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1108 :
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.
Who is online
Users browsing this forum: No registered users and 96 guests