Enums — Concept Check
Questions on Enums. Replace magic constants with type-safe enums that can carry fields, methods, and even abstract methods per constant.
Questions in this quiz
8 questions on Enums — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have an enum `Day` with constants for each day of the week. What will `Day.FRIDAY.ordinal()` return?
- A4
- B3
- C5
- D6
2.A developer uses `EnumSet` to store permissions in an application. Why might they choose `EnumSet` over `HashSet`?
- A`EnumSet` can store any object type.
- B`EnumSet` allows duplicate elements.
- C`EnumSet` is faster and more memory-efficient for enums.
- D`EnumSet` supports null elements.
3.You have an enum `Planet` with constants like `EARTH` and `MARS`, each having mass and radius fields. What is true about the constructor of this enum?
- AIt can be public and called from outside the enum.
- BIt is implicitly private and cannot be called externally.
- CIt must be protected to allow subclassing.
- DIt is package-private by default.
4.In a switch expression using an enum `Day`, what will happen if a case for `SUNDAY` is omitted?
- AThe switch will execute without any issues.
- BThe program will crash at runtime.
- CThe switch will default to the first case.
- DA compile-time error will occur.
5.Given the enum `Operation` with constants `ADD`, `SUB`, `MUL`, and `DIV`, each overriding an `apply` method, what happens if you try to add a new constant without overriding `apply`?
- AThe enum compiles but throws a runtime error.
- BThe enum compiles and runs without issues.
- CThe new constant uses a default implementation.
- DA compile-time error occurs.
6.A developer uses `EnumMap` to store descriptions for enum constants. What advantage does `EnumMap` have over `HashMap`?
- A`EnumMap` can store duplicate keys.
- B`EnumMap` automatically sorts keys alphabetically.
- C`EnumMap` is faster and more memory-efficient for enum keys.
- D`EnumMap` allows null keys.
7.What does the `name()` method of an enum constant return?
- AThe class name of the enum.
- BThe ordinal value of the constant.
- CThe hash code of the constant.
- DThe name of the constant as a String.
8.A developer incorrectly believes that `EnumSet` can store any type of object. Which of the following is true about `EnumSet`?
- A`EnumSet` can only store enum constants.
- B`EnumSet` can store primitives directly.
- C`EnumSet` can store any type of object.
- D`EnumSet` can store null values.