# AP® Computer Science A 4.2 Introduction to Using Data Sets

Source: https://lockincs.com/ap-csa/unit-4/4-2-introduction-to-using-data-sets/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 4.2.A, 4.2.A.1, 4.2.A.2, 4.2.A.3

A data set is a collection of specific pieces of information, or data. You manipulate and analyze a data set to solve a problem or answer a question by accessing its values one at a time and processing each one. A table or chart of the data helps you plan that algorithm before you write any code.

## What you will learn

- 4.2.A Represent patterns and algorithms that involve data sets found in everyday life, using written language or diagrams.

## 1\. What a data set is

A data set is a collection of specific pieces of information, or data 4.2.A.1. You meet data sets everywhere:

| Everyday data set | The pieces of data |
| --- | --- |
| A week of temperatures | 18.5, 21.0, 19.5, … |
| A class’s quiz scores | 7, 9, 10, 6, … |
| A playlist | song titles |
| A step counter | steps per day |

## 2\. Processing values one at a time

You can manipulate and analyze a data set to solve a problem or answer a question 4.2.A.2. The key pattern: the values are accessed and used one at a time, and each one is processed according to the outcome you want 4.2.A.2.

Problem: “On how many days this week did a runner run more than 5 km?” Written in plain language, the algorithm is:

1.  Set a counter to 0.
2.  Look at the next day’s distance.
3.  If that distance is greater than 5, add 1 to the counter.
4.  Repeat steps 2 and 3 until every day has been looked at.
5.  The counter is the answer.

This “visit each value and update a running result” pattern is called a traversal in code, and it is the heart of the rest of Unit 4.

## 3\. Plan with a table or chart

Data can be represented in a diagram such as a chart or table, and that visual can be used to plan the algorithm 4.2.A.3. Here is the runner’s week as a table, with an extra row that traces the counter after each value is processed:

| Day | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
| --- | --- | --- | --- | --- | --- | --- | --- |
| Distance (km) | 4.0 | 6.5 | 5.0 | 8.0 | 3.5 | 10.0 | 5.5 |
| Greater than 5? | no | yes | no | yes | no | yes | yes |
| Counter after this day | 0 | 1 | 1 | 2 | 2 | 3 | 4 |

The trace shows the answer is 4, and it also catches a common trap: 5.0 is **not** greater than 5, so Wednesday does not count.

A chart works too. A bar chart of the distances with a line drawn at 5 km lets you count the bars above the line, which is exactly what the algorithm does one bar at a time.

## 4\. From plan to code (preview)

Once the table works on paper, each written step becomes a line of code. Topic 4.3 shows how to store the data in an array; this preview shows how closely the code follows the plan.

```
double[] distances = {4.0, 6.5, 5.0, 8.0, 3.5, 10.0, 5.5};
int count = 0;                          // step 1
for (double d : distances)              // steps 2 and 4: each value, one at a time
{
    if (d > 5)                          // step 3
    {
        count++;
    }
}
System.out.println("Days over 5 km: " + count);   // step 5
```

Output:

```
Days over 5 km: 4
```

**Watch out:** the trace table is also your test. If the code prints something different from the last cell of your trace, either the plan or the code has a mistake.

## Common mix-ups

- Not true: A program looks at a whole data set at once. An algorithm accesses the values one at a time, processes each one (compare, add, count), and keeps a running result.
- Not true: Planning with a table is a waste of time; just start coding. A table of the data and a trace of the running result show the exact steps the code needs, including the starting value, which is where most bugs hide.
- Not true: A data set has to be numbers. A data set is any collection of specific pieces of information: names, words, true or false answers, temperatures or scores.

## In 30 seconds

- A data set is a collection of specific pieces of information or data.
- Data sets are manipulated and analyzed to solve a problem or answer a question.
- Values in a data set are accessed and used one at a time, then processed toward the outcome you want.
- A chart or table of the data is a diagram you can use to plan the algorithm.

## Checkpoint: 5 practice questions

### Question 1

Which of the following is best described as a data set?

- A. One variable that stores today's temperature
- B. The finishing times of every runner in a race
- C. The rule that the fastest runner wins the race
- D. The program that announces the winner of a race

**Answer: B.** A data set is a collection of specific pieces of information or data, such as many finishing times. One variable holds a single value, not a collection; a rule and a program are ways to process data, not the data itself.

### Question 2

**Data set**

| Position | 1 | 2 | 3 | 4 | 5 |
| --- | --- | --- | --- | --- | --- |
| Value | 3 | 8 | 5 | 6 | 10 |

The following algorithm is applied to the data set shown. "Set total to 0. Look at each value one at a time. If the value is even, add it to total." What is the value of total after every value has been processed?

- A. 32
- B. 3
- C. 24
- D. 14

**Answer: C.** The values are processed one at a time; the even values are 8, 6 and 10, so total is 8 + 6 + 10 = 24. 32 is the sum of all values, 3 is how many values are even, and 14 stops before the last value (8 + 6).

### Question 3

**Trace table**

| Step | 1 | 2 | 3 | 4 | 5 |
| --- | --- | --- | --- | --- | --- |
| Value | 7 | 12 | 4 | 15 | 9 |
| Largest so far | 7 | 12 | ? | ? | ? |

A student plans an algorithm that keeps the largest value seen so far, using the trace table shown. Which of the following correctly completes the last three cells of the "Largest so far" row?

- A. 4, 15, 9
- B. 12, 15, 9
- C. 7, 15, 15
- D. 12, 15, 15

**Answer: D.** The largest so far only changes when a bigger value appears: after 4 it stays 12, after 15 it becomes 15, and after 9 it stays 15. "4, 15, 9" and "12, 15, 9" copy the current value instead of keeping the largest, and "7, 15, 15" goes back to the first value.

### Question 4

A program must decide whether any reading in a week of temperatures was below 0. Which of the following written algorithms is correct?

- A. Check each reading one at a time. If a reading is below 0, the answer is yes. If none is below 0 after all are checked, the answer is no.
- B. Check only the first reading of the week. If that single reading is below 0, the answer is yes. In every other case, the answer is no.
- C. Check each reading one at a time. As soon as a reading is 0 or more, the answer is no. If that never happens, the answer is yes.
- D. Add all of the readings together into one total. If that total is below 0, the answer is yes. In every other case, the answer is no.

**Answer: A.** The values must be accessed one at a time, and one reading below 0 is enough for "yes", but "no" is only certain after every reading is checked. Checking only the first reading ignores the rest, stopping at the first reading of 0 or more answers too early, and a total can be positive even when one day was below 0.

### Question 5

**Quiz scores**

| Student | A | B | C | D | E | F |
| --- | --- | --- | --- | --- | --- | --- |
| Score | 9 | 7 | 8 | 10 | 6 | 8 |

The quiz scores of six students are shown in the table. An algorithm counts how many scores are at least 8 by checking each score one at a time. What is the final count?

- A. 3
- B. 4
- C. 5
- D. 2

**Answer: B.** The scores that are at least 8 are 9, 8, 10 and 8, so the count is 4. 3 counts only one of the two 8s, 5 also counts a score below 8, and 2 counts only the scores greater than 8 (treating "at least" as "greater than").

## Questions students ask

**What is a data set in AP® Computer Science A?**

A data set is a collection of specific pieces of information or data, such as daily temperatures, test scores or a list of names.

**How do you plan an algorithm for a data set?**

Write the data in a table, describe in words what happens to each value, and trace a running result through the table. Then turn each written step into code.

**Do I need arrays for topic 4.2?**

No. Topic 4.2 is about describing patterns and algorithms in words or diagrams. Topic 4.3 shows how to store a data set in an array.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
