Java Programming
primer-original-v2mediumJava Programming1. Introduction To Javaprimer-original-v2

Java Programming Practice Question

What will happen when the following code is compiled and run in Java 1.8? int i = 1, j = -1; switch(i) switch (i) { case 0, 1: j = 1; /* Line 4 */ case 2: j = 2; default: j = 0; } System.out.println("j = " + j);

Answer options

A
j = 0
B
j = -1
C
j = 1
D
Compilation fails. One can not specify multiple case labels with commas, as in line 4. Hence compilation error.

Correct answer: Compilation fails. One can not specify multiple case labels with commas, as in line 4. Hence compilation error.

Explanation

Quick AnswerThe correct answer is Compilation fails. One can not specify multiple case labels with commas, as in line 4. Hence compilation error. because it directly addresses the core logic of Java Programming.

This question has been retained as-is due to its unique context.

Related Java Programming questions

Practice more Java Programming questions

PrimerPrep has independently reviewed practice questions and coding exercises — all free.