ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
int[] a = {1, 2};
int[] b = {1, 2};
System.out.print(a.equals(b) + " " + java.util.Arrays.equals(a, b) + " " + java.util.Arrays.toString(a).equals(java.util.Arrays.toString(b)));
  1. A. true true true
  2. B. false false true
  3. C. false true false
  4. D. false true true

Correct answer: D. false true true

Arrays inherit Object.equals, which compares references, so a.equals(b) is false. Arrays.equals compares elements (true), and Arrays.toString gives "[1, 2]" for both, so those strings are equal (true).

More OOP with C++ and Java questions

Exams that ask this topic