AP CSA Unit 4 Day 7: Linear Search
Share
Unit 4, Data Collections • Cycle 2
Day 7 Advanced Practice • Harder Difficulty
Focus: Linear Search
Hard
Linear Search
Advanced Practice Question
Format: Linear Search
What is the output?
What is the output?
int[] data = {5, 3, 8, 1, 9, 2};
int target = 1;
int index = -1;
for (int i = 0; i < data.length; i++) {
if (data[i] == target) {
index = i;
}
}
System.out.println(index);
Difficulty: Hard |
Topic: Linear Search |
Cycle: 2 (Advanced)
Why This Answer?
Linear search finds target 1 at index 3. The loop continues after finding it but doesn't change index again, so index remains 3.
Common Mistake
Watch Out!
Thinking the loop stops immediately when item is found (without break).
AP Exam Strategy
Without break, linear search continues to the end even after finding the target.
Master This Topic
This Cycle 2 HARD question tests linear search. Review Unit 4 concepts to build mastery of data collections.
- Understanding linear search
- Tracing code execution accurately
- Avoiding common pitfalls
Ready for More Challenges?
Cycle 2 questions prepare you for the hardest AP CSA exam questions.
Study Games Practice FRQs