Mutator Methods

AP Computer Science A· difficulty 3/5

public static void modify(int[] arr) {
    arr[0] = 99;
    arr = new int[]{1, 2, 3};
    arr[1] = 50;
}
public static int caller() {
    int[] x = {10, 20, 30};
    modify(x);
    return x[0] + x[1];
}
// Call: System.out.println(caller());

What is printed?

  • A

    119

    check_circle
  • B

    51

  • C

    30

  • D

    120

Explanation

arr[0]=99 mutates x[0]. Reassigning arr locally doesn't affect x. So x = {99,20,30}. 99+20 = 119.

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

Related questions