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.
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.
colors ← ["red", "blue", "green", "gold"]
1. What is the value of colors[2]?
- (A) "red"
- (B) "blue"
- (C) "green"
- (D) An error
Show answer
(B) "blue" AP pseudocode lists start at index 1, so index 2 is "blue".
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?
- (A) The scores take fewer bits
- (B) The program can process every score with a loop, so the code is shorter and easier to change
- (C) Lists cannot contain errors
- (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.
nums ← [5, 8, 2] nums[3] ← nums[1] + nums[2]
3. What is the value of nums after the code runs?
- (A) [5, 8, 2]
- (B) [5, 8, 13]
- (C) [13, 8, 2]
- (D) [5, 8, 2, 13]
Show answer
(B) [5, 8, 13] nums[3] is replaced with 5 + 8 = 13.
pets ← ["cat", "dog", "fish"]
4. Which of the following will cause an error?
- (A) DISPLAY(pets[1])
- (B) DISPLAY(pets[3])
- (C) DISPLAY(pets[4])
- (D) DISPLAY(LENGTH(pets))
Show answer
(C) DISPLAY(pets[4]) The list has 3 elements, so index 4 is out of range.