Developing Algorithms Using Arrays

AP Computer Science A· difficulty 4/5

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

What is printed?

  • A

    100

    check_circle
  • B

    Compilation error

  • C

    0

  • D

    1

Explanation

Array references are passed by value, but the value is a reference to the same array. The method modifies the underlying array, so a[0] is now 100.

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

Related questions