Java OCA OCP Practice Question 1784

Question

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

public class Main {
  public static void main(String[] args) {
    for (int i = 0; i<10; i++) {
      switch(i) {
        case 0:/*from  www  .  j  a va 2s  .  c o  m*/
           System.out.println(i);
      }
      if (i) {
        System.out.println(i);
      }
    }
  }
}

Select the one correct answer.

  • (a) The code will fail to compile because of an illegal switch expression in the switch statement.
  • (b) The code will fail to compile because of an illegal conditional expression in the if statement.
  • (c) The code will compile without error and will print the numbers 0 through 10, when run.
  • (d) The code will compile without error and will print the number 0, when run.
  • (e) The code will compile without error and will print the number 0 twice, when run.
  • (f) The code will compile without error and will print the numbers 1 through 10, when run.


(b)

Note

The code will fail to compile, since the conditional expression of the if statement is not of type boolean.

The conditional expression of an if statement must be of type boolean.

The variable i is of type int.

There is no conversion between boolean and other primitive types.




PreviousNext

Related