nerdexam
GIAC

GSSP-JAVA · Question #8

Martin works as a Programmer in Data Net Inc. He writes the following code. 1. class Ques0191{ 2. public static void main(String[] argv){ 3. byte a = 1; 4. switch(a+1){ 5. case 1. 6…

The correct answer is B. The code will not compile because of the duplicate case label at line number 9. See the full explanation below for the reasoning.

Question

Martin works as a Programmer in Data Net Inc. He writes the following code. 1. class Ques0191{ 2. public static void main(String[] argv){ 3. byte a = 1; 4. switch(a+1){ 5. case 1. 6. System.out.println("One"); 7. case 2. 8. System.out.println("2"); 9. case 2. 10. System.out.println("Two"); break; 11. } 12. } 13. } What will happen when Martin attempts to compile and execute the code?

Options

  • AThe code will compile successfully, but a runtime error will occur because of the duplicate
  • BThe code will not compile because of the duplicate case label at line number 9.
  • CThe code will compile successfully and will execute displaying 2 followed by Two.
  • DThe code will not compile because an argument to the switch must be a constant.

How the community answered

(23 responses)
  • A
    9% (2)
  • B
    83% (19)
  • C
    4% (1)
  • D
    4% (1)

Community Discussion

No community discussion yet for this question.

Full GSSP-JAVA Practice