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?
- Acheck_circle
-1
- 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".