Searching

AP Computer Science A· difficulty 2/5

int[] a = {3, 7, 1, 9, 4};
int target = 100;
int idx = -1;
for (int i = 0; i < a.length; i++) {
  if (a[i] == target) { idx = i; break; }
}

What is idx after the loop?

  • A

    -1

    check_circle
  • B

    4

  • C

    5

  • D

    0

Explanation

Target 100 is not in the array, so idx remains its initial value of -1.

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

Related questions