Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Khalid's Mock exam engine question

 
Junaid Bhatra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one question I found in Khalid's mock exam engine, which according to me doesn't accept the correct answer.
Which statements concerning the switch statement are true?
1)All switch statements must have a default label
2)There must be exactly one label for each code segment in a switch statement
3)The keyword continue can never occur within the body of a switch statement
4)No case label may follow a default label within a single switch statement
5)A character literal can be used as a value for a case label
According to me 3 & 5 are correct. However the mock engine seems to think otherwise. Any comments?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juniad
Only 5 is correct ..see how one statment can have multiple labels...
This is an Example from Khalid

public class Seasons {
public static void main(String args[]) {
int monthNumber = 11;
switch(monthNumber) { // (1) Outer.
case 12: case 1: case 2:
System.out.println("Snow in the winter.");
break;
case 3: case 4: case 5:
System.out.println("Green grass in spring.");
break;
case 6: case 7: case 8:
System.out.println("Sunshine in the summer.");
break;
case 9: case 10: case 11: // (2)
switch(monthNumber) { // Nested switch (3) Inner.
case 10:
System.out.println("Halloween.");
break;
case 11:
System.out.println("Thanksgiving.");
break;
} // end nested switch
// Always printed for case labels 9, 10, 11
System.out.println("Yellow leaves in the fall."); // (4)
break;
default:
System.out.println(monthNumber + " is not a valid month.");
}
}
}
Wali
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wali,
Please note that I did NOT select option 2 as the correct one. All I said was that to me, both options 3 & 5 seem correct. Option 3 states that the keyword continue can never occur within the body of a switch statement.
 
ricky gonzalez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't continue take place in switch block? I think continue can be place within switch like break. Is that true?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Switch block is like if block, we can write continue in it only when it is written in any loop like for {} or While{} loop etc.
Writing continue in a separate switch block doesn't mean anything.it will just give an error at compile time.
I think it will solve others doubts ,
Thanks,
DS

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option 3 is not correct, because it is possible to have a continue statement inside a switch statement. The catch is, there must be some other structure like a while or for statement, which the continue refers to:
<code><pre> while (condition) {
switch (getValue()) {
case 1:
method1();
break;
case 2:
continue;
default:
method2();
}
method3();
}</pre></code>
The continue statement is inside a switch statement, but does not refer to it - it refers to the while loop instead. I know that isn't the situation that first comes to mind when we talk of a "continue inside a switch statement", but option 3 does say never after all, so we must be careful to consider all possibilities before we say it's true.
 
Junaid Bhatra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jim,
Thanks for replying. Yes you are right.
Just a few days ago I recall having read in JLS that a compile-time error occurs if continue appears in a switch statement. So when I saw this particular question I jumped to the conclusion without thinking about a nested for/while loop possibilty. Hmm gotta be careful in the future!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic