Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which the answer is ture? and why?

 
PANDORA
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i = 100;
switch (i) {
case 100 :
System.out.println(i);
case 200 :
System.out.println(i);
case 300 :
System.out.println(i);
}
A) Nothing is printed


B) Compile time error


C) The values 100,100,100 printed


D) Only 100 is printed
Thanks~~!
 
Dave Soto
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C) The values 100,100,100 printed
This is because there are no break statements. Without the break statements, the case 100: is executed, and then 100 is printed. Then, the case 200: is basically ignored, and 100 is printed again. Finally, the case 300: is also ignored, and so 100 is printed again.
Hope that helps!
 
PANDORA
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot
But why the case2 will be ignored?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic