Skip to content
Sharp

Modern Java · interview question

Given a 'Coordinate' value object, how can you ensure its equals method is correctly implemented?

Multiple choiceHardchapter-7-curatedpremiummodern-javavalue-objects
Choose one answer
Answer and explanation

Correct answer

A. Override equals and hashCode using both x and y fields

Why

A correct equals method for a value object should consider both x and y fields to determine equality. This ensures that two Coordinate instances are considered equal if they represent the same point in space.

Test yourself on Modern Java

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

Start free test
All Modern Java questions →