Developing Algorithms Using Arrays

AP Computer Science A· difficulty 4/5

public static void replace(int[] arr) {
  arr = new int[]{9, 9, 9};
}
public static void main(String[] args) {
  int[] a = {1, 2, 3};
  replace(a);
  System.out.println(a[0]);
}

What is printed?

  • A

    Compilation error

  • B

    0

  • C

    1

    check_circle
  • D

    9

Explanation

Reassigning the parameter arr only changes the local copy of the reference. The caller's variable a still refers to the original array, so a[0] is still 1.

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

Related questions