Introduction to Java Record and Sealed Classes
A beginner's quiz on Java's record and sealed classes.
Questions in this quiz
15 questions on Introduction to Java Record and Sealed Classes. Work through them here, then start the quiz to answer them and see your score.
1.What is a primary purpose of Java record classes?
- ATo implement complex algorithms
- BTo manage database connections
- CTo create immutable data carriers
- DTo handle exceptions
2.Which keyword is used to define a record class in Java?
- Adata
- Bsealed
- Crecord
- Dclass
3.What is a key feature of sealed classes in Java?
- AThey are always abstract
- BThey restrict which classes can extend them
- CThey automatically implement Serializable
- DThey allow inheritance from any class
4.Consider the code: 'public record Point(int x, int y) {}'. What does this code define?
- AA sealed class
- BAn immutable data class
- CAn abstract class
- DA mutable class
5.Which of the following is NOT a benefit of using record classes?
- AAutomatic generation of hashCode method
- BAutomatic generation of equals method
- CAutomatic generation of toString method
- DAutomatic generation of a default constructor
6.How do you specify permitted subclasses for a sealed class?
- AUsing 'extends' keyword
- BUsing 'extends' and 'implements' keywords
- CUsing 'permits' keyword
- DUsing 'implements' keyword
7.What happens if you try to extend a sealed class without permission?
- AIt works without issues
- BSilent failure
- CRuntime error
- DCompilation error
8.Can a record class implement interfaces?
- AYes
- BOnly if it's abstract
- COnly if it extends another class
- DNo
9.Which of the following is true about record classes?
- AThey must extend another class
- BThey can have static fields
- CThey cannot have methods
- DThey cannot implement interfaces
10.What is the default access level of fields in a record class?
- Aprotected
- Bpublic
- Cpackage-private
- Dprivate
11.Which of the following is a valid use case for sealed classes?
- AHandling file I/O
- BCreating a hierarchy of related classes
- CImplementing a logging framework
- DManaging database transactions
12.Can a sealed class be abstract?
- AYes
- BOnly if it has no fields
- COnly if it has no methods
- DNo
13.What is the main advantage of using sealed classes?
- ASimplified syntax
- BBetter control over inheritance
- CAutomatic serialization
- DImproved performance
14.Which statement is true about the constructors in record classes?
- AThey are always private
- BThey must be manually implemented
- CThey are automatically generated
- DThey cannot be defined
15.What is the purpose of the 'permits' clause in a sealed class declaration?
- ATo specify allowed interfaces
- BTo specify allowed methods
- CTo specify allowed subclasses
- DTo specify allowed fields