Java OCA OCP Practice Question 1766

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;
    Integer iRef = 5;//from   ww  w.j a  v a  2s  .  c o  m
    switch (iRef) {
      default:
        System.out.println("You are OK.");
      case 1:
      case i:
      case 2 * i:
        System.out.println("I am not OK.");
        break;
      case 4:
        System.out.println("It's OK.");
    }
  }
}

Select the one correct answer.

(a) The code will fail to compile because the type of the switch expression is not valid.
(b) The code will compile correctly and will only print the following, when run:

    You are OK./*from  www.  j  av  a 2 s  . co m*/
    I am not OK.

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

    You are OK.
    I am not OK.
    It's OK.

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

    It's OK.


(b)

Note

The switch expression, when unboxed, has the value 5.

The statement associated with the default label is executed, and the fall through is stopped by the break statement.




PreviousNext

Related