Which is true of records?
Static fields, methods and nested types are allowed.
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.
Which is true of records?
Static fields, methods and nested types are allowed.
What is a compact constructor in a record used for?
It has no parameter list; the fields are assigned automatically at the end.
A record has a component List<String> tags holding an ArrayList. What can still change?
Records are shallowly immutable.
When are two records of the same type equal by equals?
So records work well as map keys and set elements.
What does sealed interface Vehicle permits Car, Bike mean?
Other classes trying to implement Vehicle fail to compile.
Which modifiers may a permitted subclass of a sealed class use?
Records and enums are implicitly final, so they need none.
What does marking a permitted subclass non-sealed do?
The sealed parent still knows its direct subtypes.
Why can a switch over a sealed interface omit default?
Adding a new subtype then produces compile errors where it is not handled.
What does if (o instanceof Point(int x, int y)) do (Java 21)?
Record patterns can be nested and used in switch with guards.
When may the permits clause of a sealed class be omitted?
The compiler infers the permitted subclasses from the file.
Timed online tests with instant scores and explanations.