LearnFrenzy
Sign in Take a free test

Records, Enums and Sealed Classes · Exercise 1 of 4

Records, Enums and Sealed Classes Records, Enums and Sealed Classes MCQ

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

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.

Question 1 Easy

When is an enum the right choice?

A For any class with many objects
B For a type with a fixed, known set of constant values
C For mutable data
D For storing large lists
Question page & discussion
Question 2 Medium

What is the access level of an enum constructor?

A public
B private (implicitly) – enums cannot be instantiated with new
C protected
D package-private and callable from anywhere in the package
Question page & discussion
Question 3 Easy

For enum Size { S, M, L }, what is Size.L.ordinal()?

A 3
B 2
C 1
D "L"
Question page & discussion
Question 4 Medium

What does Size.valueOf("XL") do if there is no XL constant?

A Returns null
B Throws IllegalArgumentException
C Returns the first constant
D Creates a new constant
Question page & discussion
Question 5 Easy

What is the recommended way to compare two enum values?

A compareTo() == 0 only
B == (each constant is a single instance)
C Comparing their names
D Comparing hashCode()
Question page & discussion
Question 6 Medium

Which is true about enums and inheritance?

A An enum can extend any class
B An enum implicitly extends java.lang.Enum, so it cannot extend another class, but it can implement interfaces
C Enums cannot implement interfaces
D Enums can be extended by classes
Question page & discussion
Question 7 Medium

What is an advantage of EnumMap over HashMap for enum keys?

A It allows null keys
B It is array-backed, faster, more compact and iterates in declaration order
C It is thread-safe
D It sorts by value
Question page & discussion
Question 8 Medium

Why is enum Config { INSTANCE; } considered the safest singleton?

A It is faster
B The JVM guarantees a single instance, even under serialization and reflection, with thread-safe initialisation
C It can be subclassed
D It is lazily created on every call
Question page & discussion
Question 9 Easy

Which is NOT generated automatically for record Book(String title, int pages)?

A A title() accessor
B Setter methods such as setPages(int)
C equals and hashCode
D toString
Question page & discussion
Question 10 Easy

How do you read the pages component of a Book record b?

A b.getPages()
B b.pages()
C b.pages
D Book.pages(b)
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links