ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
int[][] a = {{1}, {2}};
int[][] b = a.clone();
b[0][0] = 9;
b[1] = new int[]{5};
System.out.print(a[0][0] + " " + a[1][0]);
  1. A. 9 2
  2. B. 1 2
  3. C. 9 5
  4. D. 1 5

Correct answer: A. 9 2

clone() on an array is shallow: b is a new outer array holding the SAME inner arrays. Changing b[0][0] edits the shared row, but replacing b[1] only changes b's own slot.

More OOP with C++ and Java questions

Exams that ask this topic