What is the result of the following code compilation and execution?

switch(10) {
   case 1:
       System.out.println("1");
       break;
   case 2:
       System.out.println("2");
       break;
   default:
       break;
       System.out.println("default");
}
Explanation
All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
In our case System.out.println("default"); is unreachable statement, because flow falls out of the switch statement after break statement.
Therefore compilation error will appear: "error: unreachable statement"

+Роман Викторович Thanks! Done

2015 Jul 13, 2:19:30 PM

where is explanation one?

2015 Jul 12, 6:36:53 AM

Java
Java Quiz
Start Quiz

or Read more about Java Quiz

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback