Skip to content
Sharp

Modern Java · interview question

How can you customize the toString() method in a Java Record?

Multiple choiceHardchapter-12-curatedpremiummodern-javajava-records
java
record Product(String name, double price) { @Override public String toString() { return name + " ($" + price + ")"; } }
Choose one answer
Answer and explanation

Correct answer

C. Override the toString() method in the record body

Why

You can override the toString() method within the body of a Java Record to customize its string representation. This is similar to how you would override methods in traditional classes.

Test yourself on Modern Java

A free test with instant, explained results. No account needed.

Start free test
All Modern Java questions →