AP CSA 4.4 Exercise 2: Traversing Arrays Applied Practice

Unit 4: Data Collections · Lesson 4.4 · Exercise 2

Traversing Arrays: Applied Practice

Six scenarios on choosing a loop form, stopping early, and the traversals that need to skip, pair up, or run backward.

How this one is different

The exercise-1 editor asks you to traverse. These six ask which traversal the problem actually calls for and what breaks when the bounds are copied from a different one.

Applied Practice

6 questions · scenario driven · every answer is recorded for your teacher

Question 1 of 6Apply
A method must return true as soon as it finds a negative value in an array of 100000 elements.
Which traversal is best and why?
Incorrect. Two passes cannot beat one pass that stops early.
Incorrect. Correct answer, wasted work: it still visits all 100000 elements after the answer is settled.
Correct. Correct. Returning from inside the loop is the idiomatic early exit and the point of the question.
Incorrect. Counting every negative computes far more than the question asked for.
Question 2 of 6Analyze
A traversal must compare each element to the one after it, for an array of length n.
What is the correct loop bound?
Incorrect. On the last pass arr[i + 1] is out of bounds and throws.
Correct. Correct. When the body reaches ahead, the bound must retreat by exactly as far as it reaches.
Incorrect. Even further out of bounds than i < n.
Incorrect. Halving skips most pairs. Adjacent pairs overlap; they do not partition the array.
Question 3 of 6Apply
A program must print an array in reverse order.
Which loop header is correct?
Correct. Correct. Starts at the last legal index and stops after index 0 has been printed.
Incorrect. Stops one early and never prints arr[0].
Incorrect. Starts at 0 and decrements away from the bound, immediately going negative.
Incorrect. Starts one past the end and throws on the first access.
Question 4 of 6Analyze
A loop is meant to visit only the even-numbered positions 0, 2, 4 and so on.
Which approach is most direct?
Incorrect. The enhanced for cannot express a stride, so this reintroduces an index by hand.
Incorrect. Direction is irrelevant, and starting backward changes which positions are even.
Incorrect. Correct results, but it tests a condition on every element it was never going to use.
Correct. Correct. The step belongs in the loop header when the pattern is regular.
Question 5 of 6Evaluate
A student traverses with a while loop but forgets to increment the counter inside it.
What is the result?
Incorrect. The compiler has no way to know the loop was meant to advance.
Incorrect. Nothing ends the loop after one pass; the condition is still true.
Correct. Correct. This is why the for loop gathers all three parts into its header where an omission is visible.
Incorrect. The index never changes and never leaves bounds, so no exception is thrown. It simply hangs.
Question 6 of 6Apply
A method must total only the elements strictly greater than a given threshold.
Where does the accumulator belong?
Incorrect. Declaring it inside resets it every iteration, so the result is only ever the last qualifying value.
Correct. Correct. An accumulator must outlive the iteration that updates it.
Incorrect. Same defect as A, narrowed to qualifying elements. It still resets.
Incorrect. It cannot be filled after the loop; there is nothing left to iterate over.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]