Developing Algorithms Using Arrays

AP Computer Science A· difficulty 4/5

int[] a = {1, 2, 3};
int[] b = a;
b[0] = 99;
System.out.println(a[0]);

What is printed?

  • A

    99

    check_circle
  • B

    1

  • C

    0

  • D

    Compilation error

Explanation

int[] b = a does not copy the array; it makes b refer to the same array. Modifying b[0] also changes a[0]. To make a real copy, you must allocate a new array and copy elements.

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

Related questions