Multiple choice technology programming languages

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

  1. harrier

  2. shepherd

  3. retriever

  4. Compilation fails.

  5. retriever harrier

  6. An exception is thrown at runtime.

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The switch statement contains a critical syntax error on line 18: 'case default:' is invalid. In Java switch statements, 'default' is a keyword that stands alone and cannot be combined with 'case'. The correct syntax is simply 'default:' without the word 'case' preceding it. This causes compilation to fail regardless of the enum value being tested.