Enhanced for Loop for Arrays

AP Computer Science A· difficulty 2/5

int[] a = {5, 3, 8, 1};
int total = 0;
for (int x : a) {
  total += x;
}
System.out.println(total);

What is printed?

  • A

    17

    check_circle
  • B

    16

  • C

    15

  • D

    1

Explanation

The enhanced for-loop visits every element. 5+3+8+1 = 17.

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

Related questions