When is an enum the right choice?
Days, statuses, levels, directions, payment modes…
Records, Enums and Sealed Classes · Exercise 1 of 4
Twenty multiple-choice questions on Java enums, records and sealed classes: enum methods and constructors, EnumMap, record components and compact constructors, sealed hierarchies, exhaustive switch and record patterns. Pick an option to check your answer instantly.
When is an enum the right choice?
Days, statuses, levels, directions, payment modes…
What is the access level of an enum constructor?
Declaring it public or protected is a compile error.
For enum Size { S, M, L }, what is Size.L.ordinal()?
Ordinals start at 0 in declaration order.
What does Size.valueOf("XL") do if there is no XL constant?
valueOf is also case-sensitive.
What is the recommended way to compare two enum values?
== is also null-safe, unlike calling equals on a null reference.
Which is true about enums and inheritance?
Enums are also implicitly final (unless constants have bodies, which become anonymous subclasses).
What is an advantage of EnumMap over HashMap for enum keys?
EnumSet similarly uses a bit vector.
Why is enum Config { INSTANCE; } considered the safest singleton?
Recommended by Effective Java.
Which is NOT generated automatically for record Book(String title, int pages)?
Records are immutable – no setters.
How do you read the pages component of a Book record b?
The field itself is private.
Timed online tests with instant scores and explanations.