Java OCA OCP Practice Question 1541

Question

What is the output of the following application?

package weather; 
  
public class Main { 
   public enum Size { S, M, L } 
   public static void main(String[] modelData) { 
      System.out.print(Size.S.ordinal()); 
      System.out.print(" "+Size.valueOf("flurry".toUpperCase()).name()); 
   } 
} 
  • A. 0 L
  • B. 1 L
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


A.

Note

The code compiles without issue, so Option C is incorrect.

Enum ordinal values are indexed starting with zero, so 0 is printed first.

The second line compiles and runs without issue, with flurry being converted to L, using the toUpperCase() method.

Since there is a matching enum named L, that value is printed next.

For these reasons, Option A is the correct answer.




PreviousNext

Related