Java OCA OCP Practice Question 1764

Question

What will be the result of attempting to compile and run the following program?.

public class Main {
  public static void main(String[] args) {
    final int i = 3;
    switch (6) {/* w w w  . ja v  a 2 s. c  o  m*/
      case 1:
      case i:
      case 2 * i:
        System.out.println("I am not OK.");
      default:
        System.out.println("You are OK.");
      case 4:
        System.out.println("It's OK.");
    }
  }
}

Select the one correct answer.

(a) The code will fail to compile because of the case label value 2 * i.
(b) The code will fail to compile because the default label is not specified last in the switch statement.
(c) The code will compile correctly and will only print the following, when run:
    I am not OK.// ww  w  .  j av  a 2 s.com
    You are OK.
    It's OK.

(d) The code will compile correctly and will only print the following, when run:

    You are OK.
    It's OK.

(e) The code will compile correctly and will only print the following, when run:

    It's OK.


(c)

Note

The case label value 2 * i is a constant expression whose value is 6, the same as the switch expression.

Fall through results in the printout shown in (c).




PreviousNext

Related