ProgrammingVery hardProduct-company placement · Java

What is printed?

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

Correct answer: D. false true false true

Arrays do not override equals, so a.equals(b) is identity (false). Arrays.equals compares elements with equals: fine for ints, but the inner arrays of p and q are compared by identity (false). deepEquals recurses into nested arrays.

More OOP with C++ and Java questions

Exams that ask this topic