Java OCA OCP Practice Question 2956

Question

What is the result of the following code snippet?

3: int x = 10; 
4: switch(x % 4.) { 
5:    default: System.out.print("Not divisible by 4"); 
6:    case 0: System.out.print("Divisible by 4"); 
7: } 
  • A. Not divisible by 4
  • B. Divisible by 4
  • C. Not divisible by 4Divisible by 4
  • D. The code does not output any text.
  • E. The code will not compile because of line 4.


E.

Note

For this problem you need to remember your rules about numeric promotion as well as what data types are allowed in a switch statement.

The expression x % 4.

automatically promotes the x to a double; since 4. is a double, the result is a double.

Because a switch statement does not accept the type double, the code fails to compile due to line 4, so option E is the correct answer.




PreviousNext

Related