Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Iteration
Start Practice TestPractice Test
About Exam
easy Solved by 1 students

Array Search Loop Logic

Mark
< 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 = 0;

B

arr[index]++;

C

index++;

D

index += 2;

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.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|