• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Clarification from Khalid Mughal's mock exam

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Regards,
JAI
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
Regards,
Manfred.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?!!
Nandini
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Jaikumar Nair
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
JAI
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
break;
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
default:
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.
Regards,
vkswami
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic