Java MCQ: Which of the following is true about Java’s ‘enum’ keyword?
a) ‘enum’ is used to define a collection of constants
b) Enums in Java can have methods
c) Enum constants are implicitly static and final
d) All of the above
Answer:
d) All of the above
Explanation:
In Java, the enum
keyword is used to define a collection of constants, which are essentially a fixed set of values. An enum can be thought of as a special class that holds a group of related constants, like days of the week or directions.
Enums in Java can have methods, constructors, and instance variables. However, the constants defined within an enum are implicitly static and final, meaning they cannot be changed once defined and can be accessed without creating an instance of the enum.
Understanding enums is important for representing a fixed set of known values and enhancing type safety in Java programs.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html