Java Enums MCQ

Enums, short for enumerations, are a unique kind of class in Java. They allow for a variable to be a set of predefined constants. This characteristic ensures more type safety, cleaner code, and other advantages. How familiar are you with Java enums? Let’s find out with this beginner's quiz!

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. Which of the following best describes an enum in Java?

a) A special kind of interface
b) A type of method
c) A way to declare arrays
d) A special class that represents a group of constants

Answer:

d) A special class that represents a group of constants

Explanation:

Enums are special classes in Java used to define collections of constants.

2. How do you define an enum that represents days of the week?

a) enum DAYS {MON, TUE, WED, THU, FRI, SAT, SUN}
b) enum DAYS [MON, TUE, WED, THU, FRI, SAT, SUN]
c) Days enum = {MON, TUE, WED, THU, FRI, SAT, SUN}
d) enum DAYS

Answer:

a) enum DAYS {MON, TUE, WED, THU, FRI, SAT, SUN}

Explanation:

Enums are defined using the enum keyword followed by the enum name and constants enclosed in curly braces.

3. Which method can be used to get the ordinal value of an enum constant?

a) getValue()
b) getOrdinal()
c) getOrder()
d) ordinal()

Answer:

d) ordinal()

Explanation:

The ordinal() method of an enum returns the ordinal value (position) of the enum constant, starting from 0.

4. Can an enum have a constructor?

a) Yes, and it can be public
b) Yes, but it must always be private
c) No, enums cannot have constructors
d) Yes, but it must be protected

Answer:

b) Yes, but it must always be private

Explanation:

Enums can have constructors, but they are always private. This is to prevent the creation of new enum instances.

5. Can you override the toString() method in an enum?

a) Yes
b) No
c) Only in abstract enums
d) Only if the enum has attributes

Answer:

a) Yes

Explanation:

Just like any other class in Java, you can override the toString() method in an enum to provide a custom string representation of the enum constant.

6. Which method can be used to get an enum constant by its string name?

a) Enum.valueOf()
b) Enum.getByName()
c) Enum.get()
d) Enum.fromString()

Answer:

a) Enum.valueOf()

Explanation:

The static valueOf() method in the enum can be used to retrieve an enum constant by its string name.

7. Can enums extend other classes in Java?

a) Yes
b) No
c) Only abstract classes
d) Only other enums

Answer:

b) No

Explanation:

Enums cannot extend other classes in Java because they implicitly extend the java.lang.Enum class. However, they can implement interfaces.

8. What happens when you try to print an enum constant directly?

a) It throws an error
b) It prints the ordinal value
c) It prints the name of the constant
d) It prints the fully qualified class name of the enum constant

Answer:

c) It prints the name of the constant

Explanation:

The default toString() method in enums returns the name of the constant. So when you print an enum constant directly, it shows the constant's name.

9. Can an enum be declared inside a class?

a) Yes, and it is always static
b) No
c) Only if the class is abstract
d) Only in the main class

Answer:

a) Yes, and it is always static

Explanation:

An enum can be declared inside a class, and when it's done this way, it's implicitly static.

10. Which of the following statements about enums is NOT correct?

a) Enums can implement interfaces
b) Enums can have attributes and methods
c) Enums can be instantiated using the new keyword
d) Enums can have constructors

Answer:

c) Enums can be instantiated using the new keyword

Explanation:

Enums cannot be instantiated using the new keyword. The constants defined in an enum are the only instances that can exist for that enum type.

Enums provide a concise and type-safe way to represent a fixed set of related constants in Java. They are versatile and can be employed in a variety of use cases, from simple value collections to sophisticated type patterns. If you found this quiz informative, continue delving deeper into Java's features and enhance your understanding. Happy coding!



Comments