jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clarification from Khalid Mughal's mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clarification from Khalid Mughal Watch "Clarification from Khalid Mughal New topic

Clarification from Khalid Mughal's mock exam

Jaikumar Nair
Ranch Hand

Joined: Apr 04, 2001
Posts: 48
Following is one of the questions from the mock test of Khalid Mughal :
Which statements concerning the switch construct are true?
(a) all switch stmts must have a default label
(b) there must be exactly one label for each code segment in a switch statement
(c) the keyword continue can never occur within the body of a switch statement
(d) no case label may follow a default label within a single switch stmt
(e) a character literal can be used as a value for a case label
According to the mock exam "e" is the only correct answer.
But I am quite sure that "c" is also the correct answer. Could someone comment on this.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Jaikumar,
Check out the following code.

Notice the continue statement inside the switch statement?
Try to compile and run it ...
P.S. The continue keyword can be used within loops only!
Nandini Gangopadhyay
Ranch Hand

Joined: Apr 02, 2001
Posts: 79
I too think c is a correct answer, if we give a continue, where will it take us, there is no new iteration taking place here. Could someone else clarify too?!!
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Per the JLS 14.15 The continue Statement

A continue statement may occur only in a while, do, or for statement; statements of these three kinds are called iteration statements. Control passes to the loop-continuation point of an iteration statement.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Jaikumar Nair
Ranch Hand

Joined: Apr 04, 2001
Posts: 48
Hi Manfred,
I am aware of that and the example that you provided will run.
The option "c" states "the keyword continue can never occur within the body of a switch statement". Is it not a bit ambiguous? Lets take the example you have given. In the code, though "continue" is within the "for...loop", it is appearing within the body of a switch statement. In that respect, option "c" should be true. Do correct me if I am wrong
vkswamy venkatachalam

Joined: Feb 18, 2001
Posts: 21
Hi, jayakumar
Answer c may be wrong.The compiler fails to compile only when
there is 'continue' key word only inside a case.Try the following code.
public class Test1 {
public static void main(String args[]) {
int i = 5;
loop:for(int j=0;j<10;j++){
switch( i ){
case 4:
case 5:
//inserted for knowing what is going inside.
//if U uncomment, donot forget to add { }
//{System.out.println("Inside case 5 : for loop i : "+i);
continue loop;
//if continue without loop and for loop the compiler rejects
System.out.println("Hello World!");
System.out.println("end of for loop i : "+i);
the variant of continue works here.so the otion 'c' may be wrong.
Cindy Glass, correct me if I am wrong.

I agree. Here's the link: http://aspose.com/file-tools
subject: Clarification from Khalid Mughal's mock exam
It's not a secret anymore!