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 2 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 11 Medium

Which is true of records?

A They can extend other classes
B They are implicitly final and extend java.lang.Record, but may implement interfaces
C They can be abstract
D They can declare instance fields
Question page & discussion
Question 12 Medium

What is a compact constructor in a record used for?

A Creating a copy of the record
B Validating or normalising the component values before they are assigned to the fields
C Adding extra fields
D Making the record mutable
Question page & discussion
Question 13 Hard

A record has a component List<String> tags holding an ArrayList. What can still change?

A Nothing
B The list's contents – the record only fixes the reference; copy it with List.copyOf in the compact constructor
C The reference itself
D The record's class
Question page & discussion
Question 14 Easy

When are two records of the same type equal by equals?

A Only if they are the same object
B When all their components are equal
C When their hash codes are equal
D Never
Question page & discussion
Question 15 Easy

What does sealed interface Vehicle permits Car, Bike mean?

A Vehicle cannot be used
B Only Car and Bike may directly implement Vehicle
C Car and Bike are final automatically
D Vehicle has no methods
Question page & discussion
Question 16 Medium

Which modifiers may a permitted subclass of a sealed class use?

A public, private or protected
B final, sealed or non-sealed
C static or abstract only
D None
Question page & discussion
Question 17 Medium

What does marking a permitted subclass non-sealed do?

A Seals it further
B Re-opens that branch: any class may extend it
C Makes it abstract
D Removes it from the hierarchy
Question page & discussion
Question 18 Medium

Why can a switch over a sealed interface omit default?

A default is always optional
B The compiler knows all permitted subtypes and checks that each is handled
C Sealed types cannot be null
D Only records are allowed
Question page & discussion
Question 19 Medium

What does if (o instanceof Point(int x, int y)) do (Java 21)?

A Creates a new Point
B Tests whether o is a Point and, if so, extracts its components into x and y
C Compares o with the origin
D Casts o to int
Question page & discussion
Question 20 Hard

When may the permits clause of a sealed class be omitted?

A Never
B When all permitted subclasses are declared in the same source file
C When there is only one subclass
D When the class is abstract
Question page & discussion
Up next · Exercise 2Predict the Output20 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links