AP CSP
AP Computer Science Principles / All lessons / Big Idea 3: Algorithms and Programming
3Big Idea 3: Algorithms and Programming
AP CSP topic 3.2

Data Abstraction

A list is a single variable that holds many values in order, like a list of test scores. Each value is an element, and you reach it by its index. In AP pseudocode, list indexes start at 1, so scores[1] is the first element. Lists are a form of data abstraction: you work with the whole collection by name without managing each value separately, which makes programs shorter and easier to change.

Picture it90index 185index 272index 3100index 464index 5grades
In AP pseudocode, list indexes start at 1. grades[3] is 72. LENGTH(grades) is 5.

Data Abstraction vocabulary

  • List: An ordered collection of elements stored under one name.
  • Element: One value inside a list.
  • Index: The position number of an element; AP pseudocode starts at 1.
  • Data abstraction: Giving a collection of data one name so it can be used without knowing every detail.

AP-style practice questions

Try each one, then open the answer. To save your progress, take this quiz in the interactive course.

Skill: Code Analysis
colors ← ["red", "blue", "green", "gold"]

1. What is the value of colors[2]?

  1. (A) "red"
  2. (B) "blue"
  3. (C) "green"
  4. (D) An error
Show answer

(B) "blue" AP pseudocode lists start at index 1, so index 2 is "blue".

Skill: Abstraction in Program Development

2. A program stores 30 test scores. Which of the following is the main benefit of storing them in one list instead of 30 separate variables?

  1. (A) The scores take fewer bits
  2. (B) The program can process every score with a loop, so the code is shorter and easier to change
  3. (C) Lists cannot contain errors
  4. (D) Lists run faster than variables in every case
Show answer

(B) The program can process every score with a loop, so the code is shorter and easier to change A list is a data abstraction that lets one loop handle all the values.

Skill: Code Analysis
nums ← [5, 8, 2]
nums[3] ← nums[1] + nums[2]

3. What is the value of nums after the code runs?

  1. (A) [5, 8, 2]
  2. (B) [5, 8, 13]
  3. (C) [13, 8, 2]
  4. (D) [5, 8, 2, 13]
Show answer

(B) [5, 8, 13] nums[3] is replaced with 5 + 8 = 13.

Skill: Code Analysis
pets ← ["cat", "dog", "fish"]

4. Which of the following will cause an error?

  1. (A) DISPLAY(pets[1])
  2. (B) DISPLAY(pets[3])
  3. (C) DISPLAY(pets[4])
  4. (D) DISPLAY(LENGTH(pets))
Show answer

(C) DISPLAY(pets[4]) The list has 3 elements, so index 4 is out of range.

Open 3.2 in the interactive course