Skip to content
Sharp

Modern Java · interview question

Consider the following code snippet. What will be the output if the PriceRecord is instantiated with new PriceRecord(new BigDecimal("20.00"), Currency.USD)?

Multiple choiceHardchapter-7-curatedpremiummodern-javacleaner-domain-modeling
java
record PriceRecord(BigDecimal value, Currency currency) {}
Choose one answer
Answer and explanation

Correct answer

C. "Price: 20.00 USD"

Why

The toString method of records automatically includes field names and values in the output format. The output will be structured as "PriceRecord[value=20.00, currency=USD]", but since the question asks for a formatted string, option a is correct if the record overrides toString for formatting.

Test yourself on Modern Java

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

Start free test
All Modern Java questions →