Java OCA OCP Practice Question 2936

Question

What is the result of the following code snippet?

3: int x = 10 % 2 + 1; 
4: switch(x) { 
5:    case: 0 System.out.print("Too High"); break; 
6:    case: 1 System.out.print("Just Right"); break; 
7:    default: System.out.print("Too Low"); 
8: } 
A.   Too High
B.   Just Right
C.   Too Low
D.   JustRightTooLow
E.   The code will not compile because of line 3.
F.   The code will not compile because of lines 5 and 6.


F.

Note

This question is designed to test your ability to spot syntax errors with switch statements.

In particular, the colon (:) goes after the value in the case statement, not before.

Therefore neither line 5 nor line 6 will compile, and option F is the correct answer.

If the colon were moved after the values, the output would be Just Right, and the answer would be option B.




PreviousNext

Related