Given: 12. public class Test { 13. public enum Dogs {collie, harrier}; 14. public static void main(String [] args) { 15. Dogs myDog = Dogs.collie; 16. switch (myDog) { 17. case collie: 18. System.out.print(”collie “); 19. case harrier: 20. System.out.print(”harrier “); 21. } 22. } 23. } What is the result?

  1. collie

  2. harrier

  3. Compilation fails

  4. collie harrier


Correct Option: D

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) collie - This option is incorrect. If we follow the code execution, we see that the switch statement matches the value of myDog with the cases. The value of myDog is Dogs.collie, so it matches the case collie on line 17. However, there is no break statement after the System.out.print("collie ") on line 18, so the execution continues to the next case.

Option B) harrier - This option is incorrect. As explained in Option A, after the execution of the System.out.print("collie ") on line 18, there is no break statement, so the execution continues to the next case. Therefore, the output will include both "collie" and "harrier".

Option C) Compilation fails - This option is incorrect. The code provided does not contain any compilation errors.

Option D) collie harrier - This option is correct. As explained in Option A and Option B, the output will include both "collie" and "harrier". So the correct answer is D.

The correct answer is D.

Find more quizzes: