Developing Algorithms Using Arrays

AP Computer Science A· difficulty 2/5

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

What is printed?

  • A

    2 4 6

    check_circle
  • B

    1 2 3

  • C

    0 0 0

  • D

    2 2 2

Explanation

Indexed assignment a[i] = a[i] * 2 modifies the array. Each element is doubled: 2, 4, 6.

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

Related questions