Java OCA OCP Practice Question 2186

Question

What is the output of the following application?

1:  package mypkg; 
2:  enum Season { 
3:     SPRING(1), SUMMER(2), FALL(3), WINTER(4) 
4:     public Season(int orderId) {} 
5:  } // w  w  w  .ja v a2  s  .c o m
6:  public class Main { 
7:     public static void main(String... orchard) { 
8:        final Season s = Season.FALL; 
9:        switch(s) { 
10:          case 3: 
11:             System.out.println("Start"); 
12:          default: 
13:             System.out.println("End"); 
14:       } 
15:    } 
16: } 
  • A. Start
  • B. Start followed by End
  • C. One line of code does not compile.
  • D. Two lines of code do not compile.
  • E. Three lines of code do not compile.
  • F. The code compiles but prints an exception at runtime.


E.

Note

The code does not compile, so Options A, B, and F are incorrect.

The first compilation error is on line 3, which is missing a required semicolon (;) at the end of the line.

A semicolon (;) is required at the end of any enum value list if the enum contains anything after the list, such as a method or constructor.

The next compilation error is on line 4 because enum constructors cannot be public.

The last compilation error is on line 10.

The case statement must use an enum value, such as FALL, not an int value.

For these three reasons, Option E is the correct answer.




PreviousNext

Related