Java OCA OCP Practice Question 2558

Question

What is the result of the following code?

public class Main { 
   enum Letter { 
      A, B, C 
   } 
   public static void main(String[] args) { 
      System.out.println(Letter.B.ordinal()); 
   } 
} 
  • A. 0
  • B. 1
  • C. 9
  • D. B
  • E. The code does not compile due to a missing semicolon.
  • F. The code does not compile for a different reason.


B.

Note

The ordinal() method of an enum returns its corresponding int value.

Like arrays, enums are zero based.

Remember that the index of an enum may change when you recompile the code and should not be used for comparison.




PreviousNext

Related