OCA Java SE 8 Operators/Statements - OCA Mock Question Operator and Statement 3


What is the result of the following code snippet?

     3: final char a = 'A', d = 'D'; 
     4: char grade = 'B'; 
     5: switch(grade) { 
     6:   case  a: 
     7:   case 'B': System.out.print("B"); 
     8:   case 'C': System.out.print("C"); break; 
     9:   case  d: 
     10:  case 'F': System.out.print("F"); 
     11: } 
  1. B
  2. BC
  3. The code will not compile because of line 3.
  4. The code will not compile because of line 6.
  5. The code will not compile because of lines 6 and 9.




The value of grade is 'B' and there is a matching case statement and it prints "B".

After that there is no break statement, so the next case statement executes and outputs "C".

There is a break after this case statement, so the switch statement will end.

// w  ww  .j ava 2 s  .c  o  m
public class Main{
   public static void main(String[] argv){
        final char a = 'A', d = 'D'; 
        char grade = 'B'; 
        switch(grade) { 
          case  a: 
          case 'B': System.out.print("B"); 
          case 'C': System.out.print("C"); break; 
          case  d: 
          case 'F': System.out.print("F"); 

The code above generates the following result.