The Stacks
BIG IDEA 3: ALGORITHMS AND PROGRAMMING · TOPIC 3.2

3.2 Data Abstraction

A list lets one name refer to many related values. That's data abstraction, and it's one of the two abstraction types the Create Task explicitly grades.

What you need to know

  • Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation. Practically: a list lets you treat a whole collection as one thing.
  • A list is an ordered sequence of elements. Each element is referenced by its index. In AP pseudocode, indexing starts at 1, so the first element is list[1] and the last is list[LENGTH(list)].
  • Lists manage complexity: instead of score1, score2, … score30, you use one list scores and loop over it. Adding a 31st score requires no code changes.
  • The Create Task requires you to identify a list in your program and explain how it manages complexity — this exact phrase. The answer is about handling many values with one name and code that works regardless of how many there are.
  • A string is also an ordered sequence (of characters), and a list can contain strings, numbers, Booleans, or even other lists.
  • Using a list in place of many separate variables makes the program easier to develop, maintain, and read.

Worked example

Without a list, averaging five test scores means five variables and a formula that must be rewritten if a sixth test is added. With a list:

scores ← [88, 92, 79, 95, 84]
total ← 0
FOR EACH s IN scores
{
    total ← total + s
}
DISPLAY(total / LENGTH(scores))

Add a sixth score to the list and nothing else changes. That's the "manages complexity" argument the Create Task wants.

Exam tip: Index questions are a constant source of lost points. AP pseudocode is 1-indexed. list[0] is an error. If a question shows real-language code (Python-style), it will say so — otherwise assume 1-based.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • The CED phrase is that data abstraction "provides a separation between the abstract properties of a data type and the concrete details of its representation." In plain terms: you think of scores as "the list of scores" and don't think about where each number lives in memory.
  • A list can hold different types in AP pseudocode — numbers, strings, Booleans, even other lists — though most exam lists are one type. A list of lists is how you'd model a grid.
  • The Create Task requires that your list (a) is used to store data and (b) that the program's functionality would be harder or impossible without it. The written response asks you to explain how the list manages complexity. The expected answer: without the list, you'd need a separate variable for each value, and code would have to change every time the number of values changed.
  • Index starts at 1 in AP pseudocode. list[1] is the first element, list[LENGTH(list)] is the last. Every real language you'll use starts at 0 — the exam is the exception, and it's deliberate: they test whether you read the reference sheet.
  • Lists are ordered: the position of each element is meaningful and stable. Two lists with the same elements in different orders are different lists.
  • A string is conceptually an ordered list of characters, but AP pseudocode doesn't let you index into strings the way you index lists. The exam defines any string operations it needs within the question.

Mistakes that cost points

  • Index 0. list[0] in AP pseudocode is an error. The first element is list[1]. This single fact is tested on nearly every exam.
  • Explaining the list too vaguely on the Create Task. "The list stores my data" doesn't earn the point. You need: what the list holds, and why the program couldn't do its job without it (or would need many separate variables).
  • Confusing data abstraction with procedural abstraction. A list is data abstraction. A procedure is procedural abstraction. Both "manage complexity," but they're different mechanisms and the Create Task asks about them separately.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 In AP pseudocode, what is displayed by the following code?
names ← ["Ava", "Ben", "Cy", "Dee"]
DISPLAY(names[3])
  1. A Ava
  2. B Ben
  3. C Cy
  4. D Dee
Show answer

Answer: C. Indexing starts at 1, so names[3] is the third element, "Cy".

Q2 A student's program stores each of 40 book titles in a separate variable. Which of the following changes would best manage the complexity of the program?
  1. A Rename each variable with a shorter name.
  2. B Store the titles in a single list and use a loop to process them.
  3. C Add a comment above each variable.
  4. D Convert the titles to uppercase.
Show answer

Answer: B. Replacing many variables with one list is the CED's definition of how data abstraction manages complexity.

Key vocabulary

Data abstraction
using a structure like a list so a collection of values can be treated as one unit
List
an ordered sequence of elements, each accessed by index
Element
a single value in a list
Index
the position of an element in a list; starts at 1 in AP pseudocode