Array Creation and Access

AP Computer Science A· difficulty 1/5

int[] a = new int[5];
int sum = 0;
for (int x : a) {
  sum += x;
}
System.out.println(sum);

What is printed?

  • A

    ArrayIndexOutOfBoundsException

  • B

    5

  • C

    null

  • D

    0

    check_circle

Explanation

new int[5] initializes all 5 elements to 0. Their sum is 0.

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

Related questions