Java OCA OCP Practice Question 2522

Question

What is the result of the following code?

1:    public class Main { 
2:       enum Letter { 
3:          A, B, C; 
4:       } 
5:       public static void main(String[] args) { 
6:          Letter[] animals = Letter.values(); 
7:          System.out.println(animals[1]); 
8:       } 
9:    } 
  • A. B
  • B. A
  • C. The code compiles, but the output is indeterminate.
  • D. A compiler error occurs on line 2.
  • E. A compiler error occurs on line 6.
  • F. A compiler error occurs on line 7.


A.

Note

The code compiles.

An enum may be an inner class.

The values() method returns an array with the enum values in the order in which they were declared in the code.

Since Java uses 0-based indexes, the answer is A.




PreviousNext

Related