Searching

AP Computer Science A· difficulty 2/5

int[] a = {2, 5, 2, 7, 2, 8};
int count = 0;
for (int x : a) {
  if (x == 2) count++;
}

What is count?

  • A

    4

  • B

    2

  • C

    3

    check_circle
  • D

    1

Explanation

The value 2 appears at indices 0, 2, and 4, so count is 3.

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

Related questions