PrepGo

AP Computer Science Principles Practice Quiz: Lists

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 16 questions to check your progress.

Question 1 of 16

Consider the list `scores ← [88, 92, 75, 100]`. What is the result of evaluating the expression `scores[3]`, assuming 1-based indexing?

All Questions (16)

Consider the list `scores ← [88, 92, 75, 100]`. What is the result of evaluating the expression `scores[3]`, assuming 1-based indexing?

A) 92

B) 75

C) 100

D) An error, as the index is out of bounds.

Correct Answer: B

Based on list indexing, `scores[3]` accesses the third element of the list. In a 1-based indexing system as used on the exam reference sheet, the first element is at index 1, the second at index 2, and the third at index 3. The value at index 3 is 75.

A list `colors` is initialized as `["red", "green", "blue"]`. The following operation is performed: `APPEND(colors, "yellow")`. What is the state of the `colors` list after this operation?

A) ["yellow", "red", "green", "blue"]

B) ["red", "green", "blue", "yellow"]

C) ["red", "green", "yellow", "blue"]

D) ["yellow"]

Correct Answer: B

The `APPEND` procedure, as described in the exam reference sheet basic list operations, adds an element to the end of a list. Therefore, "yellow" is added after "blue", resulting in the list `["red", "green", "blue", "yellow"]`.

Consider the list `items ← ["A", "B", "D", "E"]`. What is the content of the list after the command `INSERT(items, 3, "C")` is executed, assuming 1-based indexing?

A) ["A", "B", "C", "D", "E"]

B) ["A", "B", "D", "C", "E"]

C) ["A", "C", "B", "D", "E"]

D) ["A", "B", "C"]

Correct Answer: A

The `INSERT(list, i, value)` procedure inserts the `value` at index `i`, shifting subsequent elements to the right. Inserting "C" at index 3 places it in the third position and moves the existing elements "D" and "E" to indices 4 and 5, respectively.

A list `data` is defined as `[10, 20, 30, 40, 50]`. What is the list `data` after the operation `REMOVE(data, 2)` is performed, assuming 1-based indexing?

A) [10, 30, 40, 50]

B) [20, 30, 40, 50]

C) [10, 20, 40, 50]

D) [10, 20, 30, 40]

Correct Answer: A

The `REMOVE(list, i)` procedure removes the element at index `i`. In this case, the element at index 2 is 20. Removing it from the list results in `[10, 30, 40, 50]`.

Consider the following code segment: `numbers ← [5, 10, 15, 20]` `sum ← 0` `FOR EACH num IN numbers` `{` ` sum ← sum + num` `}` `DISPLAY(sum)` What value is displayed as a result of running this code?

A) 20

B) 30

C) 50

D) 15

Correct Answer: C

This algorithm uses a `FOR EACH` loop for a complete traversal of the list. It initializes `sum` to 0 and, for each element, adds the element's value to `sum`. The final sum is 5 + 10 + 15 + 20 = 50. This demonstrates using an iteration statement to traverse a list to compute a sum.

Consider the following code segment: `values ← [2, 4, 6, 8, 10]` `count ← 0` `FOR EACH val IN values` `{` ` IF (val > 5)` ` {` ` count ← count + 1` ` }` `}` `DISPLAY(count)` What is displayed when this code segment is executed?

A) 2

B) 3

C) 4

D) 5

Correct Answer: B

This question requires determining the result of an algorithm that includes a list traversal. The code traverses the list `values`. The `count` variable is incremented only for elements greater than 5. The elements 6, 8, and 10 meet this condition. Therefore, `count` is incremented three times, and the final value displayed is 3.

A linear search algorithm is used to find the value 45 in the list `[10, 25, 45, 60, 80]`. How many elements will be checked before the value is found?

A) 1

B) 2

C) 3

D) 5

Correct Answer: C

A linear (or sequential) search algorithm checks each element in order until the value is found. It will check 10 (first element), then 25 (second element), and then 45 (third element). Since the value is found at the third position, 3 elements are checked.

Consider the following code segment: `grades ← [85, 92, 78, 95, 88]` `maxGrade ← 0` `FOR EACH grade IN grades` `{` ` IF (grade > maxGrade)` ` {` ` maxGrade ← grade` ` }` `}` `DISPLAY(maxGrade)` What is the output of this code segment?

A) 0

B) 85

C) 92

D) 95

Correct Answer: D

This algorithm finds the maximum value in a list. It initializes `maxGrade` to 0 and then traverses the list using a `FOR EACH` loop. It updates `maxGrade` each time it finds a value in the list that is larger than the current `maxGrade`. The largest value in the `grades` list is 95, which will be the final value displayed.

Which of the following scenarios best describes a partial traversal of a list?

A) Calculating the average of all numbers in a list of test scores.

B) Searching for the first occurrence of a specific name in a list of attendees and stopping once it is found.

C) Displaying every item in a shopping list.

D) Counting the total number of elements in a list using a `FOR EACH` loop.

Correct Answer: B

A partial traversal accesses only a portion of the list's elements. A linear search that stops once the target is found is a prime example, as it does not need to check the remaining elements. The other options describe complete traversals where all elements must be accessed to get the correct result.

Consider the following sequence of operations on a list named `myList`, assuming 1-based indexing: 1. `myList ← [10, 20, 40]` 2. `INSERT(myList, 3, 30)` 3. `APPEND(myList, 50)` 4. `REMOVE(myList, 1)` What is the final state of `myList`?

A) [20, 30, 40, 50]

B) [20, 40, 30, 50]

C) [10, 20, 30, 50]

D) [20, 40, 50]

Correct Answer: A

This question requires evaluating expressions that use list procedures. 1. Start: `[10, 20, 40]` 2. `INSERT(myList, 3, 30)` results in `[10, 20, 30, 40]`. 3. `APPEND(myList, 50)` results in `[10, 20, 30, 40, 50]`. 4. `REMOVE(myList, 1)` removes the first element (10), resulting in `[20, 30, 40, 50]`.

A linear search algorithm is designed to check each element of a list in order. If the algorithm is used to find the value 50 in the list `[10, 20, 30, 40]`, what is the outcome?

A) The algorithm returns the last element, 40.

B) The algorithm reports that the value was not found after checking all elements.

C) The algorithm results in an error because the value is not in the list.

D) The algorithm returns the first element, 10.

Correct Answer: B

The definition of a linear search states it checks each element 'until the value is found or all elements are checked.' Since 50 is not in the list, the algorithm will perform a complete traversal, check all four elements, and then conclude that the value is not present.

A programmer wants to write an algorithm to count how many numbers in a list `numList` are even. Which code segment correctly implements this algorithm?

A) `evenCount ← 0` `FOR EACH num IN numList` `{` ` IF ((num MOD 2) = 0)` ` {` ` evenCount ← evenCount + 1` ` }` `}`

B) `evenCount ← 0` `FOR EACH num IN numList` `{` ` IF ((num MOD 2) = 1)` ` {` ` evenCount ← evenCount + 1` ` }` `}`

C) `evenCount ← 1` `FOR EACH num IN numList` `{` ` IF ((num MOD 2) = 0)` ` {` ` evenCount ← evenCount + 1` ` }` `}`

D) `evenCount ← 0` `FOR EACH num IN numList` `{` ` evenCount ← evenCount + 1` `}`

Correct Answer: A

This question requires writing an iteration statement to traverse a list. To count even numbers, a counter (`evenCount`) should be initialized to 0. The `FOR EACH` loop correctly traverses the list. Inside the loop, the condition `(num MOD 2) = 0` correctly identifies even numbers, and the counter is incremented. Option B counts odd numbers, Option C initializes the counter incorrectly, and Option D counts all numbers.

Consider the following code segment, which uses 1-based indexing: `data ← [5, 10, 15, 20]` `temp ← data[2]` `data[2] ← data[4]` `data[4] ← temp` What is the content of the list `data` after this code is executed?

A) [5, 10, 15, 20]

B) [5, 20, 15, 10]

C) [20, 10, 15, 5]

D) [5, 15, 10, 20]

Correct Answer: B

This code demonstrates assigning an element's value to a variable and assigning a value to an element. It swaps the elements at index 2 and 4. 1. `temp` is assigned `data[2]`, so `temp` becomes 10. 2. `data[2]` is assigned `data[4]`, so the list becomes `[5, 20, 15, 20]`. 3. `data[4]` is assigned `temp`, so the list becomes `[5, 20, 15, 10]`.

The following algorithm is intended to compute the average of the numbers in a list. `numbers ← [10, 15, 20, 25]` `sum ← 0` `<MISSING CODE>` `average ← sum / LENGTH(numbers)` `DISPLAY(average)` Which of the following should replace `<MISSING CODE>` for the algorithm to work correctly?

A) `FOR EACH num IN numbers` `{` ` sum ← sum + num` `}`

B) `FOR EACH num IN numbers` `{` ` sum ← num` `}`

C) `sum ← LENGTH(numbers)`

D) `FOR EACH num IN numbers` `{` ` sum ← sum + 1` `}`

Correct Answer: A

Computing an average requires summing the elements and dividing by the count. The missing code must calculate the sum. The `FOR EACH` loop in option A correctly traverses the list and accumulates the sum of its elements in the `sum` variable. This is a common algorithm pattern using iteration.

According to the provided content, which programming structure is explicitly mentioned for iterating over all elements of a list?

A) A `WHILE` loop

B) A `REPEAT...UNTIL` loop

C) A `FOR EACH` loop

D) A recursive function

Correct Answer: C

The content explicitly states, 'The exam reference sheet provides the FOR EACH loop structure for iterating over all elements of a list.' This structure is designed for a complete traversal of a list.

A programmer attempts to find the minimum value in a list of positive numbers with the following code: `scores ← [90, 85, 95, 75, 100]` `minScore ← 100` `FOR EACH score IN scores` `{` ` IF (score < minScore)` ` {` ` minScore ← score` ` }` `}` `DISPLAY(minScore)` What value is displayed?

A) 100

B) 90

C) 85

D) 75

Correct Answer: D

This question requires determining the result of a given algorithm. The algorithm traverses the list to find the minimum value. - `minScore` starts at 100. - It is updated to 90 (since 90 < 100). - It is updated to 85 (since 85 < 90). - It is not updated for 95 (since 95 is not < 85). - It is updated to 75 (since 75 < 85). - It is not updated for 100 (since 100 is not < 75). The final value of `minScore` that is displayed is 75.