What is the result?

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?

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?

A.
harrier

B.
shepherd

C.
retriever

D.
Compilation fails.

E.
retriever harrier

F.
An exception is thrown at runtime.

Explanation:
Main.java:8: illegal start of expression
case default:
^
Main.java:8: illegal start of expression
case default:
^
Main.java:9: ‘;’ expected
System.out.print(“retriever “);
^
3 errors



Leave a Reply 1

Your email address will not be published. Required fields are marked *