AP CSA 4.2 Exercise 2: Introduction to Using Data Sets Applied Practice

Unit 4: Data Collections · Lesson 4.2 · Exercise 2

Introduction to Using Data Sets: Applied Practice

Six scenarios on why a collection is worth having at all, and what the enhanced for loop can and cannot do to one.

How this one is different

The exercise-1 editor asks you to loop over a data set. These six ask when a loop over a collection is the right tool and what the enhanced for gives up in exchange for its clean syntax.

Applied Practice

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

Question 1 of 6Analyze
A program tracks 500 temperature readings. A student proposes 500 separate variables named temp1 through temp500.
What is the decisive argument for a data set instead?
Correct. Correct. A collection gives you indexing, and indexing is what makes iteration possible. That is the whole reason the structure exists.
Incorrect. Variable names may contain digits. temp1 is perfectly legal.
Incorrect. Element access is not meaningfully faster than variable access. Speed is not the argument.
Incorrect. Memory use is roughly the same. The problem is not space.
Question 2 of 6Apply
A student writes: for (int t : temps) { t = t + 1; } and is surprised the array is unchanged.
Why did the array not change?
Incorrect. Array elements are freely modifiable through an index.
Incorrect. A standard for loop with an index works; the loop type matters only because it gives access to the index.
Incorrect. The loop executed once per element. It simply had no lasting effect.
Correct. Correct. This is the defining limitation of the enhanced for loop and the most common misuse of it.
Question 3 of 6Evaluate
A method must report the position of the first reading above 100, not just whether one exists.
Which loop form is required?
Incorrect. Both loop forms can stop early with break or a flag.
Incorrect. Readability does not help when the required value is unavailable.
Correct. Correct. Needing the index is the standard test for which loop form to reach for.
Incorrect. The enhanced for hands you the value only. Any position it reported would have to be counted manually, which is just an index by another name.
Question 4 of 6Analyze
Two programs process the same 10000 readings. One prints a running average as it goes; the other stores every reading and computes the average at the end.
What does the second program gain for its extra memory?
Incorrect. It is not strictly worse. It trades memory for the ability to revisit data.
Correct. Correct. A running statistic cannot be un-run. Keeping the set is what makes order-dependent questions answerable.
Incorrect. Both compute the average in a single pass over the same count of values.
Incorrect. Storing values does not prevent overflow; the sum can overflow either way.
Question 5 of 6Apply
A data set is described as "a collection of related values processed as a unit".
Which of these is NOT a data set in that sense?
Correct. Correct. One value is a value. There is nothing to iterate over and no collection to process as a unit.
Incorrect. An ArrayList of related names is a data set held in a resizable structure.
Incorrect. A 2D array is a data set whose elements happen to be arranged in rows and columns.
Incorrect. An array of related totals processed together is exactly a data set.
Question 6 of 6Evaluate
A student says "the enhanced for loop is always better because it is shorter and cannot go out of bounds".
What is the accurate refinement of that claim?
Incorrect. It is clearly preferable for straightforward read-only traversal, which is the common case.
Incorrect. It cannot go out of bounds. The loop manages its own traversal.
Incorrect. "Always" is what makes it wrong. The loop has a real and specific range of applicability.
Correct. Correct. Both halves matter: it genuinely removes a class of bug, and it genuinely gives up capability to do so.

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]