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
record PriceRecord(BigDecimal value, Currency currency) {}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.
More Modern Java questions
- Given an enum with a method, how can each constant have a different implementation of that method?
- What keyword is used to define a Java Record?
- Which pattern is effectively implemented when using enums to represent singletons?
- How can you iterate over all constants of an enum in Java?
- When designing a domain model, why should you be cautious about using records for classes with business logic?
- How can the use of enums improve the clarity of domain models in Java?