ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
// Java 16 or later
record P(int x, int y) { }
// in main:
P a = new P(1, 2), b = new P(1, 2);
System.out.print((a == b) + " " + a.equals(b) + " "
    + (a.hashCode() == b.hashCode()) + " " + a.x());
  1. A. true true true 1
  2. B. false false false 1
  3. C. false true false 1
  4. D. false true true 1

Correct answer: D. false true true 1

Records get component-based equals and hashCode and an accessor method x(), but new still creates two distinct objects, so == is false.

More OOP with C++ and Java questions

Exams that ask this topic