Java OCA OCP Practice Question 2930

Question

What is the result of the following code snippet?

public class Main {

   public static void main(String[] args) throws Exception {
      final char a = 'A', d = 'D'; // p1 
      char grade = 'B'; 
      switch(grade) { 
         case a:  // p2 
         case 'B': System.out.print("great"); 
         case 'C': System.out.print("good"); break; 
         case d:  // p3 
         case 'F': System.out.print("not good"); 
      } /*from  www  .j  a  v a  2 s. co  m*/
   }
}
A.  great
B.  greatgood
C.  The code will not compile because of line p1.
D.  The code will not compile because of line p2.
E.  The code will not compile because of lines p2 and p3.


B.

Note

The code compiles and runs without issue, so options C, D, and E are not correct.

The value of grade is 'B', and there is a matching case statement that will cause "great" to be printed.

There is no break statement after the case, though, so the next case statement will be reached and "good" will be printed.

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

The correct answer is thus option B.




PreviousNext

Related