Skip to content
క
kagitha
/
quiz
/
java-records
Subscribe
Index
⌘K
JAVA NOTEBOOK · QUIZ 04 · BEGINNER
Java records
QUESTION 01 / 07
Given record Employee(String name, String designation) {}, how do you read the name?
A
employee.getName()
B
employee.name()
C
employee.name
D
employee.get("name")
SCORE SO FAR · 0 / 0
Share this quiz ↗
Given record Employee(String name, String designation) {}, how do you read the name?
— employee.name(). Record accessors are named after the component, with no get prefix. The field itself is private.
Which of these does the compiler NOT generate for a record?
— A setter for each component. Records are immutable. Their fields are private and final, so there are no setters. To change a value you create a new record.
Is new Employee("Asha", "Architect").equals(new Employee("Asha", "Architect")) true or false?
— True, because records compare their components. A record's equals and hashCode are generated from all its components, so two records with the same data are equal. That makes them safe as HashMap keys.
You want Salary to reject a negative amount. Where does that check go?
— In a compact constructor: Salary { if (amount < 0) throw ... }. A compact constructor has no parameter list and runs before the fields are assigned, so it can check and even normalise the values.
record Team(String name, List<Employee> members) {}. Is a Team fully immutable?
— No, the List itself can still be changed unless you copy it, e.g. List.copyOf(members). Record immutability is shallow. The field can't be reassigned, but a mutable List can still be modified. A defensive List.copyOf in the compact constructor fixes it.
Where is a record a poor fit?
— A JPA / Hibernate entity. JPA entities need a no-argument constructor, mutable fields and often proxies. Records have none of these. Use records for the DTOs around your entities.
What does Java 21 let you write with a record Point(int x, int y)?
— if (obj instanceof Point(int x, int y)) { ... }. Java 21 record patterns check the type and pull out the components in one step, in instanceof and in switch.
kagitha
Close
✕
Search everything — articles, videos, chapters, agents, books
01
AI
02
Java
03
System Design
04
Series
05
Manga
06
The Lab
07
Videos
08
Quizzes
09
Books
10
Resume