Chapter FIVE
Enumerations


Exam Objectives

Use enumerated types including methods, and constructors in an enum type.

Answers

1. The correct answer is C.
An enum is implicitly static. This makes the printValue() method execute in a static context. So, the reference in the method printValue() to the variable value is invalid, because value is non-static.


2. The correct answer is A.
There's nothing wrong with the code. Since values() returns an array of enums in the same order in which they were declared, Blue is returned, and 1 is printed.


3. The correct answer is A.
Option A is true. Enums are thread-safe.
Option B is false. Enums can't extend from a class, but they can implement an interface.
Option C is false. Enums can define private constructors.
Option D is false. Enums can have setter methods.


4. The correct answer is C.
Enums are created just in time, the first time they are referenced. When variable l1 is created, the enum Level is also created, calling the constructor of HIGH and LOW and printing 100 and 10.


5. The correct answer is B.
Enums constants from different enumerations are not equal, even if they share the same name.


6. The correct answers are A and B.
Option A is true. You can compare two enumerations using the == operator.
Option B is true. Enums implicitly inherit from java.lang.Enum.
Option C is false. You can use the new operator inside an enum, but you cannot use the new operator to create a reference to an enum.
Option D is false. You can have abstract methods inside an enum, but all enum constants must provide an implementation.