Array Search Loop Logic
The following code segment is intended to find the first occurrence of the value 3 in the array arr. Which statement should replace // missing line so that the code works as intended?
public class Search {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int index = 0;
while (index < arr.length && arr[index] != 3) {
// missing line
}
if (index < arr.length) {
System.out.println("Found 3 at index " + index);
} else {
System.out.println("3 not found");
}
}
}
A
index++;
B
index = 0;
C
index += 2;
D
arr[index]++;
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE