Java Record Classes & Immutability: Beginner's Quiz
Test your understanding of Java record classes and their immutability features.
Questions in this quiz
15 questions on Java Record Classes & Immutability: Beginner's Quiz. Work through them here, then start the quiz to answer them and see your score.
1.What is a key characteristic of a Java record class?
- AThey require explicit implementation of all methods.
- BThey are mutable by default.
- CThey cannot have fields.
- DThey automatically generate hashCode, equals, and toString methods.
2.Which keyword is used to define a record class in Java?
- Astruct
- Brecord
- Cclass
- Dimmutable
3.How do you declare a record class named 'Point' with fields 'x' and 'y'?
- Arecord Point(int x, int y) {}
- BPoint record(int x, int y) {}
- Cstruct Point(int x, int y) {}
- Dclass Point(int x, int y) {}
4.Can a record class in Java have instance methods?
- ANo, they cannot have any methods.
- BYes, they can have instance methods.
- CYes, but only private methods.
- DYes, but only static methods.
5.What is the default nature of fields in a Java record class?
- AImmutable
- BMutable
- CStatic
- DVolatile
6.Which method is automatically provided by a Java record class?
- Anotify()
- Bfinalize()
- Cclone()
- DtoString()
7.Can you add custom constructors to a Java record class?
- AYes, but they must call the canonical constructor.
- BYes, without any restrictions.
- CNo, constructors are not allowed.
- DOnly private constructors are allowed.
8.What happens if you try to modify a field in a Java record class?
- AA runtime exception is thrown.
- BThe modification is ignored.
- CThe field is updated.
- DA compile-time error occurs.
9.Which of the following is NOT a benefit of using record classes?
- AIncreased mutability
- BAutomatic method generation
- CReduced boilerplate code
- DBetter readability
10.Can record classes implement interfaces?
- ANo, they cannot implement interfaces.
- BYes, they can implement interfaces.
- CYes, but only marker interfaces.
- DYes, but only one interface.
11.What is the purpose of the canonical constructor in a record class?
- ATo prevent instantiation.
- BTo enforce immutability.
- CTo initialize all fields.
- DTo provide default values for fields.
12.Which of the following statements about Java record classes is true?
- AThey are a special kind of class.
- BThey are mutable by default.
- CThey cannot have static fields.
- DThey can extend other classes.
13.What is the result of calling the equals method on two record objects with the same field values?
- Afalse
- BIt throws an exception.
- Cnull
- Dtrue
14.Can a record class have static methods?
- AYes, but only one static method.
- BNo, static methods are not allowed.
- CYes, they can have static methods.
- DYes, but they must be private.
15.Which of the following is a limitation of Java record classes?
- AThey cannot have instance methods.
- BThey cannot extend other classes.
- CThey cannot implement interfaces.
- DThey cannot be instantiated.