| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Iteration
Start Practice TestPractice Test
About Exam
easy Solved by 2 students
Array Search Loop Logic
< Prev
Next >

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]++;

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy