Java OCA OCP Practice Question 1522

Question

Given the following enum declaration, how many lines contain compilation errors?

package mypkg; 

enum MyBase {} 

public enum Color extends MyBase { 
   RED, BLUE, ORANGE, GREEN 
   protected Color() {} 
} 
  • A. None, the code compiles as is.
  • B. One
  • C. Two
  • D. Three


D.

Note

The program contains three compilation problems.

The enum Color extends the enum MyBase, but enums cannot extend other enums so the definition is invalid.

The enum value list must end with a semicolon (;) if the enum definition contains anything other than the enum values.

Since it includes a constructor, a semicolon (;) is required after GREEN.

enum constructors must be private, meaning the protected constructor for Color does not compile.

Option D is the correct answer.




PreviousNext

Related