Java OCA OCP Practice Question 108

Question

Given:

1. enum Pet { //  www .ja v  a 2 s .co m
2.   DOG("woof"), CAT("meow"), FISH("burble"); 
3.   String sound; 
4.   Pet(String s) { sound = s; } 
5. } 
6. class TestEnum { 
7.   static Pet a; 
8.   public static void main(String[] args) { 
9.     System.out.println(a.DOG.sound + " " + a.FISH.sound); 
10.   } 
11. } 

What is the result?

  • A. woof burble
  • B. Multiple compilation errors
  • C. Compilation fails due to an error on line 2
  • D. Compilation fails due to an error on line 3
  • E. Compilation fails due to an error on line 4
  • F. Compilation fails due to an error on line 9


A is correct; enums can have constructors and variables.

Note

B, C, D, E, and F are incorrect; these lines all use correct syntax.




PreviousNext

Related