ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
// Java 16 or later
record R(int[] a) { }
record S(List<Integer> l) { }
// in main:
System.out.print(new R(new int[]{1}).equals(new R(new int[]{1})) + " "
    + new S(List.of(1)).equals(new S(List.of(1))));
  1. A. false true
  2. B. true true
  3. C. false false
  4. D. true false

Correct answer: A. false true

A record's generated equals compares each component with that component's own equals. Arrays inherit identity equals from Object, so two different arrays differ; Lists compare by content.

More OOP with C++ and Java questions

Exams that ask this topic