Java OCA OCP Practice Question 1500

Question

What is the output of the following application?

package mypkg; /* w  w  w.j a v a2  s.c o  m*/
enum Holiday { 
   Thanksgiving, PresidentsDay, Christmas 
} 
public class Vacation { 
   public static void main(String... unused) { 
      final Holiday input = Holiday.Thanksgiving; 
      switch(input) { 
         default: 
         case Holiday.Christmas: 
            System.out.print("1"); 
         case Holiday.PresidentsDay: 
            System.out.print("2"); 
      } 
   } 
} 
  • A. 1
  • B. 2
  • C. 12
  • D. None of the above


D.

Note

The application contains a compilation error.

The case statements incorrectly use the enum name as well as the value, such as Holiday.

Since the type of the enum is determined by the value of the variable in the switch statement, the enum name is not allowed and throws a compilation error when used.

Option D is correct.

If the enum name Holiday was removed, the application would output 12, since the lack of any break statements causes multiple blocks to be reached, and Option C would have been the correct answer.




PreviousNext

Related