What is the output of the following code?
class Rectangle { int marks = 10; } public class Main { public static void main(String... args) { Rectangle s = new Rectangle(); switch (s.marks) { default: System.out.println("100"); case 10: System.out.println("10"); case 98: System.out.println("98"); } } } a 100 10 98 b 10 98 c 100 d 10
B
The default case executes only if no matching values are found.
A matching value of 10 is found and the case label prints 10.
Because a break statement doesn't terminate this case label, the code execution continues and executes the remaining statements within the switch block, until a break statement terminates it or it ends.
class Rectangle { int marks = 10; } public class Main { public static void main(String... args) { Rectangle s = new Rectangle(); switch (s.marks) { default: System.out.println("100"); case 10: System.out.println("10"); case 98: System.out.println("98"); } /* w ww . ja va 2s .c om*/ } }
The code above generates the following result.