Developing Algorithms Using Arrays

AP Computer Science A· difficulty 3/5

int[] a = {1, 2, 3};
int[] b = new int[a.length];
for (int i = 0; i < a.length; i++) {
  b[i] = a[i];
}
b[0] = 99;
System.out.println(a[0] + " " + b[0]);

What is printed?

  • A

    1 99

    check_circle
  • B

    0 99

  • C

    1 1

  • D

    99 99

Explanation

A new array is allocated and elements are copied one-by-one. Now a and b refer to different arrays, so modifying b[0] does not affect a[0].

Want 10 more like this — adaptive to your weak spots?

Related questions