Developing Algorithms Using Arrays

AP Computer Science A· difficulty 4/5

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

What is printed?

  • A

    1

    check_circle
  • B

    99

  • C

    0

  • D

    Compilation error

Explanation

Because b is a separate array with copied values, modifying a[0] does not affect b[0]. b[0] is still 1.

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

Related questions