Java Record & Sealed Classes: Beginner's Guide
Test your understanding of Java's record and sealed classes with this beginner-level quiz.
Questions in this quiz
15 questions on Java Record & Sealed Classes: Beginner's Guide. Work through them here, then start the quiz to answer them and see your score.
1.What is a primary characteristic of a Java record?
- AIt is a subclass of String
- BIt is a subclass of Object
- CIt is mutable
- DIt is immutable
2.Which keyword is used to define a sealed class in Java?
- Afinal
- Bstatic
- Csealed
- Dabstract
3.What is the purpose of a sealed class?
- ATo restrict which classes can extend it
- BTo enable multiple inheritance
- CTo make the class immutable
- DTo allow any class to extend it
4.Which of the following is a correct way to declare a record in Java?
- Aenum Point(int x, int y) {}
- Bclass Point(int x, int y) {}
- Cinterface Point(int x, int y) {}
- Drecord Point(int x, int y) {}
5.Can a sealed class have non-sealed subclasses?
- AYes
- BOnly if they are final
- CNo
- DOnly in Java 8
6.What is automatically provided by a Java record?
- AA toString method
- BA custom equals method
- CA default constructor
- DA finalize method
7.Which of the following is NOT a valid modifier for a sealed class?
- Anon-sealed
- Babstract
- Cfinal
- Dstatic
8.How do you specify permitted subclasses for a sealed class?
- AUsing the 'implements' keyword
- BUsing the 'permits' clause
- CUsing the 'extends' keyword
- DUsing the 'inherits' keyword
9.What is a key benefit of using records in Java?
- AThey enhance encapsulation
- BThey allow multiple inheritance
- CThey provide dynamic method dispatch
- DThey simplify the creation of data-carrying classes
10.Which of the following can extend a sealed class?
- AAny class
- BOnly classes specified in the permits clause
- COnly interface
- DOnly abstract classes
11.What happens if you try to extend a sealed class without being listed in the permits clause?
- AThe code compiles but throws an exception at runtime
- BThe code runs but with a warning
- CThe code compiles and runs normally
- DThe code fails to compile
12.Can a record in Java have methods?
- ANo, records cannot have methods
- BYes, but only static methods
- CYes, but only private methods
- DYes, records can have instance methods
13.Which of the following is true about records and inheritance?
- ARecords cannot extend other classes
- BRecords can extend other classes
- CRecords can implement interfaces
- DRecords can only extend abstract classes
14.What is a common use case for sealed classes?
- ATo create utility classes
- BTo implement singleton patterns
- CTo manage database connections
- DTo define a closed set of subclasses
15.Can a record have a custom constructor?
- AYes, but only if they are private
- BYes, but only if they are static
- CYes, records can have custom constructors
- DNo, records cannot have constructors