Return to site
JAVA CERTIFICATION QUESTION: Using the arrow in Java’s switch expressions
👉Behavioral differences between statements and expressions can be subtle.
Given the following enum and the main method:
enum Direction { UP, DOWN, HOLD; } … public static void main(String[] args) { var dir = Direction.UP; switch(dir) { case UP -> System.out.print("up "); case DOWN -> System.out.print("down"); // line n1 } }
Which statement is true? Choose one.
A. The code compiles and prints up.
B. The code compiles and prints up down.
C. The code fails to compile. To fix it you must add the following after line n1:
case HOLD -> System.out.println("hold");
D. The code fails to compile. To fix it you must add the following after line n1:
default -> System.out.println("default");
·Ɐ sᴉ ɹǝʍsuɐ ʇɔǝɹɹoɔ ǝɥꓕ