Searching

AP Computer Science A· difficulty 2/5

int[] a = {1, 2, 3, 4};
int target = 99;
int idx = -1;
for (int i = 0; i < a.length; i++) {
  if (a[i] == target) {
    idx = i;
  }
}
System.out.println(idx);

What is printed?

  • A

    -1

    check_circle
  • B

    4

  • C

    0

  • D

    ArrayIndexOutOfBoundsException

Explanation

99 is not in the array, so idx remains its initial value of -1. The convention -1 indicates "not found".

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

Related questions