What is an enum in Java?
A
A class that stores multiple values B
A special data type to define a collection of constants C
A type of loop D
A method modifier
Analysis & Theory
An enum is a special data type used to define collections of constant values.
How do you declare an enum in Java?
A
using the `enum` keyword B
using the `class` keyword C
using the `interface` keyword D
using the `final` keyword
Analysis & Theory
Enums are declared using the `enum` keyword.
Which of these is a valid enum declaration?
A
enum Day { MON, TUE, WED } B
enum Day = { MON, TUE, WED } C
enum Day (MON, TUE, WED); D
enum Day: MON, TUE, WED;
Analysis & Theory
The correct syntax uses curly braces without assignment.
What is the superclass of all enums in Java?
A
Object B
Enum C
Class D
Interface
Analysis & Theory
All enums implicitly extend the `java.lang.Enum` class.
How can you get all constants of an enum as an array?
A
Using values() method B
Using getAll() method C
Using enumConstants() method D
Using getValues() method
Analysis & Theory
The `values()` method returns all enum constants in an array.
Can enums have constructors in Java?
A
Yes, but they must be private or package-private B
No, enums cannot have constructors C
Yes, but only public constructors D
Only if they implement interfaces
Analysis & Theory
Enums can have constructors, but they are always private or package-private.
Can enums have methods in Java?
A
Yes B
No C
Only static methods D
Only abstract methods
Analysis & Theory
Enums can have instance methods, static methods, and override methods like `toString()`.
What will `Day.MONDAY.ordinal()` return?
A
0 B
1 C
MONDAY D
An error
Analysis & Theory
`ordinal()` returns the zero-based position of the enum constant.
How to compare two enum constants?
A
Using == operator B
Using equals() method only C
Using compareTo() only D
Using both == and equals()
Analysis & Theory
Both `==` and `equals()` can be used to compare enum constants.
Which of these is NOT a benefit of using enums?
A
Type safety B
Code readability C
Performance improvement D
Ability to store multiple values per constant
Analysis & Theory
Enums improve readability and type safety but do not inherently improve performance.