Records Flashcards
“What is the primary purpose of records in Java?
a) To create immutable data classes
b) To improve array processing speed
c) To enhance dynamic method binding
d) To manage memory allocation”
a) To create immutable data classes
“In a Java record, how are fields defined?
a) Using private fields with setters
b) Using a compact constructor
c) Directly in the record header
d) With protected visibility”
c) Directly in the record header
“Which keyword is used to declare a record in Java?
a) data
b) class
c) record
d) struct”
c) record
“What is the default nature of fields in a Java record?
a) Mutable
b) Private and mutable
c) Public and mutable
d) Private and final”
d) Private and final
“How does a record handle methods like equals(), hashCode(), and toString()?
a) It automatically generates them based on fields
b) It requires the programmer to define each manually
c) It does not support these methods
d) It generates them only if explicitly overridden”
a) It automatically generates them based on fields
“What happens if you try to create a setter method in a record?
a) It will override the default behavior
b) It will throw a compilation error
c) It will allow only one mutable field
d) The record will ignore the setter method”
b) It will throw a compilation error
“What is the primary benefit of using records for data classes?
a) Reduces code for boilerplate methods
b) Increases inheritance capabilities
c) Enhances dynamic type safety
d) Improves runtime performance”
a) Reduces code for boilerplate methods
“Can a record implement interfaces in Java?
a) Yes, but it cannot override methods
b) No, records cannot implement interfaces
c) Yes, a record can implement interfaces like any other class
d) Only if the interface has no methods”
c) Yes, a record can implement interfaces like any other class
“What type of constructor is typically used in a Java record?
a) Default constructor
b) No-arg constructor
c) Compact constructor
d) Protected constructor”
c) Compact constructor
“In a record, what does the compact constructor allow you to do?
a) Define custom field assignments
b) Overload the equals() method
c) Change field mutability
d) Add setter methods”
a) Define custom field assignments