AP CSA 4.5 Exercise 2: Algorithms with Arrays Applied Practice

Unit 4: Data Collections · Lesson 4.5 · Exercise 2

Algorithms with Arrays: Applied Practice

Six scenarios on the standard array algorithms, and the difference between reporting a value and reporting where it lives.

How this one is different

The exercise-1 editor asks you to implement min, max and count. These six ask what breaks when the initial value is chosen badly and when an index has to be tracked alongside the value.

Applied Practice

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

Question 1 of 6Analyze
A maximum-finding method initializes int max = 0; then compares each element against it.
On which input does this fail?
Incorrect. An empty array is a separate problem, and it also breaks the arr[0] seed. It is not the only failure here.
Incorrect. It fails on all-negative data, which is exactly why the constant seed is a mutant worth naming.
Incorrect. With any positive value present, 0 is beaten immediately and the result is right.
Correct. Correct. Seeding with a constant assumes the data cannot fall below it. Seeding with arr[0] makes no such assumption.
Question 2 of 6Apply
A method must report the POSITION of the largest value, not the value itself.
What must the algorithm track?
Incorrect. A count answers a different question entirely.
Incorrect. The value alone cannot say where it came from.
Correct. Correct. The index is the stronger piece of information; tracking it alone is sufficient and avoids the two variables drifting out of step.
Incorrect. Workable but redundant, and redundancy here is a chance for the pair to disagree after a careless edit.
Question 3 of 6Analyze
Two maximum-finders differ only in using > versus >= when comparing to the best so far, and both return the largest INDEX.
Do they behave identically?
Incorrect. They agree on the maximum value but can disagree on where it is.
Correct. Correct. The distinction becomes observable precisely because an index is reported rather than a value.
Incorrect. Both compile; both are valid comparisons.
Incorrect. Emptiness affects both identically and is not what separates them.
Question 4 of 6Apply
A method computes the average of an int array as sum / arr.length, with both operands int.
What goes wrong?
Correct. Correct. The fix is to cast before dividing, not after: casting the result of an int division has already lost the remainder.
Incorrect. Overflow requires very large totals and is a separate concern.
Incorrect. The count of elements is the right denominator for a mean.
Incorrect. It is correct only when the total divides evenly, which is the exception rather than the rule.
Question 5 of 6Evaluate
A method must decide whether every element satisfies a condition.
Which structure is correct and efficient?
Incorrect. Correct results but it always scans the whole array, even after a failure has settled the answer.
Incorrect. One element says nothing about the rest.
Incorrect. That answers "does any element satisfy it", a different question.
Correct. Correct. A universal claim is refuted by one counterexample, so the loop hunts for the counterexample.
Question 6 of 6Analyze
An array algorithm must run on an array that might have length 0.
Which algorithm needs a guard before it starts?
Incorrect. True of the loops, but the arr[0] seed happens before the loop and still throws.
Incorrect. The accumulator starts at 0 and the loop body never runs. The answer is meaningfully 0.
Correct. Correct. Any algorithm that reads an element BEFORE the loop needs an element to exist.
Incorrect. Same as summing: a zero-initialized counter is already the right answer.

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]