nerdexam
Oracle

1Z0-851 · Question #149

Given: 1. public class Test { 2. public enum Dogs {collie, harrier, shepherd}; 3. public static void main(String [] args) { 4. Dogs myDog = Dogs.shepherd; 5. switch (myDog) { 6. case collie: 7…

The correct answer is D. Compilation fails. See the full explanation below for the reasoning.

Question

Given: 1. public class Test { 2. public enum Dogs {collie, harrier, shepherd}; 3. public static void main(String [] args) { 4. Dogs myDog = Dogs.shepherd; 5. switch (myDog) { 6. case collie: 7. System.out.print("collie "); 8. case default: 9. System.out.print("retriever "); 10. case harrier: 11. System.out.print("harrier "); 12. } 13. } 14.} What is the result?

Options

  • Aharrier
  • Bshepherd
  • Cretriever
  • DCompilation fails.
  • Eretriever harrier
  • FAn exception is thrown at runtime.

How the community answered

(28 responses)
  • B
    4% (1)
  • D
    82% (23)
  • E
    4% (1)
  • F
    11% (3)

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice