wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Khalid's Mock exam engine question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Khalid Watch "Khalid New topic

Khalid's Mock exam engine question

Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
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?
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.");
case 3: case 4: case 5:
System.out.println("Green grass in spring.");
case 6: case 7: case 8:
System.out.println("Sunshine in the summer.");
case 9: case 10: case 11: // (2)
switch(monthNumber) { // Nested switch (3) Inner.
case 10:
case 11:
} // end nested switch
// Always printed for case labels 9, 10, 11
System.out.println("Yellow leaves in the fall."); // (4)
System.out.println(monthNumber + " is not a valid month.");
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
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

Joined: Jun 30, 2000
Posts: 167
Why can't continue take place in switch block? I think continue can be place within switch like break. Is that true?
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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 ,

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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:
case 2:
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.

"I'm not back." - Bill Harding, Twister
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
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!
jQuery in Action, 2nd edition
subject: Khalid's Mock exam engine question
Similar Threads
switch statement question
Clarification from Khalid Mughal's mock exam
switch & case + for loop
switch doubt..