Lists

Help Questions

AP Computer Science Principles › Lists

Questions 1 - 10
1

A student grades list of integers starts as 90, 84, 76, 88, 95. A teacher tries to add 10 points to the second grade, like a bonus for extra credit. The code mistakenly uses grades2 = grades2 + 10 while intending the second element. The list uses zero-based indexing. Only one element should be updated. Based on the scenario above, identify the error in the list operation and suggest a correction.

Change to grades.remove(1)

Change to grades[2] = grades[2] + '10'

Change to grades[1] = grades[1] + 10

Change to grades.append(10)

Explanation

This question tests understanding of list operations in programming, specifically the relationship between element position and zero-based indexing. In programming, lists are used to store collections of items. Common operations include adding, removing, and accessing elements using indices. In this scenario, the teacher wants to modify the second grade but uses grades[2], which actually accesses the third element due to zero-based indexing. Choice A is correct because grades[1] properly targets the second element (index 1 corresponds to the second position in zero-based indexing). Choice B is incorrect because it adds a string '10' instead of the integer 10, which would cause a type error when adding to an integer, showing confusion between data types. To help students: Create position-to-index conversion charts, practice identifying 'off-by-one' errors, and use consistent language distinguishing between 'position' (1st, 2nd) and 'index' (0, 1). Emphasize checking index values before operations.

2

A shopping cart list of strings starts as 'milk','bread','eggs','apples','rice'. The shopper removes the item 'eggs' after deciding not to buy it, like taking it out of a real basket. The code uses cart.remove('eggs') to delete that exact element. The list keeps the remaining items in the same order. No other items are added or changed. In the context of the problem, how does the list change after removing the element?

['milk','bread','apples','rice']

['milk','bread','eggs','apples','rice']

['milk','bread','apples','rice','eggs']

['eggs','milk','bread','apples','rice']

Explanation

This question tests understanding of list operations in programming, specifically the remove() method for deleting elements by value. In programming, lists are used to store collections of items. Common operations include adding, removing, and accessing elements using indices. In this scenario, the shopping cart list ['milk','bread','eggs','apples','rice'] is modified through the remove() operation to delete 'eggs'. Choice B is correct because the remove() method deletes the first occurrence of the specified value ('eggs'), resulting in ['milk','bread','apples','rice'], with all remaining items maintaining their original order. Choice C is incorrect because it shows 'eggs' moved to the beginning rather than removed, which often occurs when students confuse remove() with other list operations. To help students: Emphasize that remove() deletes elements by value (not index), practice with visual representations of lists before and after operations, and use real-world analogies like removing items from a physical shopping cart. Have students trace through each operation step-by-step to predict outcomes.

3

A song playlist list of strings starts as 'Blue','Gold','Neon','Pulse','River'. You remove one song by name, like deleting a track from a queue. The code runs playlist.remove('Neon') to delete that exact title. The remaining songs keep their current order. No other edits occur. In the context of the problem, how does the list change after removing the element?

['Blue','Gold','Pulse','River','Neon']

['Neon','Blue','Gold','Pulse','River']

['Blue','Gold','Neon','Pulse','River']

['Blue','Gold','Pulse','River']

Explanation

This question tests understanding of list operations in programming, specifically the remove() method for deleting elements by value. In programming, lists are used to store collections of items. Common operations include adding, removing, and accessing elements using indices. In this scenario, the playlist ['Blue','Gold','Neon','Pulse','River'] has the song 'Neon' removed using playlist.remove('Neon'). Choice A is correct because remove() deletes the specified element ('Neon') from its current position, resulting in ['Blue','Gold','Pulse','River'] with remaining elements maintaining their relative order. Choice C is incorrect because it shows 'Neon' still in the list but moved to a different position, which often occurs when students confuse remove() with reordering operations like sort() or insert(). To help students: Use visual demonstrations showing elements disappearing rather than moving, practice predicting list contents after remove() operations, and emphasize that remove() permanently deletes the first occurrence of a value. Create exercises comparing remove() with other list methods.

4

A to-do list of strings starts as 'Pack','Print tickets','Charge phone','Lock door','Leave'. You try to mark the second task done with tasks5 = 'DONE', but the list has only five items. In the context of the problem, identify the error in the list operation and suggest a correction.

Use tasks.append('DONE')

Use tasks['1'] = 'DONE'

Use tasks[5] = 'DONE' again

Use tasks[1] = 'DONE'

Explanation

This question tests understanding of list operations in programming, specifically index bounds and error correction. In programming, list indices must be within valid bounds (0 to length-1), and attempting to access index 5 in a 5-element list causes an error since valid indices are 0-4. In this scenario, the to-do list has 5 elements, so tasks[5] is out of bounds when trying to mark the second task as done. Choice A is correct because tasks[1] = 'DONE' properly accesses the second element (index 1) within the valid range. Choice B is incorrect because it suggests using the same invalid index again, showing a misunderstanding of the index bounds issue. To help students: Emphasize zero-based indexing and valid index ranges, practice identifying index errors, and use debugging exercises where students fix out-of-bounds errors. Teach students to check list length before accessing indices.

5

A student grades list of integers starts as 88, 92, 76, 95, 84. After a re-check, you update the third grade using grades2 = 80. In the context of the problem, what is the output after executing the following list operation: grades2?

Index 5 is accessed

92

76

80

Explanation

This question tests understanding of list operations in programming, specifically updating elements using index notation. In programming, list elements can be modified by assigning new values to specific indices, where indexing starts at 0. In this scenario, the grades list [88, 92, 76, 95, 84] is modified by updating grades[2] = 80, which changes the third element (index 2) from 76 to 80. Choice B is correct because grades[2] now contains the value 80 after the assignment operation. Choice A is incorrect because it shows the original value 76, indicating a misunderstanding that assignment doesn't change the stored value. To help students: Practice with zero-based indexing, use visual representations showing index positions, and emphasize that assignment replaces the existing value completely. Have students verify their understanding by predicting values at different indices after updates.

6

A sensor readings list of integers starts as 72, 74, 73, 75, 71. You transfer data and then reset by setting readings = []. In the context of the problem, what is the output after executing the following list operation: len(readings)?

5

"0"

0

1

Explanation

This question tests understanding of list operations in programming, specifically the length of an empty list. In programming, lists can be emptied by assigning an empty list [], and the len() function returns the number of elements currently in the list. In this scenario, the sensor readings list is reset to an empty list using readings = [], which removes all elements. Choice A is correct because len([]) returns 0, as an empty list contains zero elements regardless of its previous contents. Choice C is incorrect because it represents the original list length, indicating a misunderstanding that assignment doesn't affect the list's current state. To help students: Emphasize that assignment with = completely replaces the list contents, practice with the len() function on lists of various sizes including empty lists, and distinguish between modifying a list and replacing it entirely. Use debugging exercises where students predict len() outputs for different list states.

7

A song playlist list of strings starts as 'Intro','Skyline','Drive','Echoes','Finale'. You add a new song at the end, like adding a track to the end of a mixtape. The goal is to use append so the new title becomes the last element. No other songs move or get removed. The list remains in the same order otherwise. Based on the scenario above, which line of code correctly adds an item to the list?

playlist.append('New Song')

playlist[0] = 'New Song'

playlist.remove('New Song')

playlist['New Song'].append()

Explanation

This question tests understanding of list operations in programming, specifically the append() method for adding elements to the end of a list. In programming, lists are used to store collections of items. Common operations include adding, removing, and accessing elements using indices. In this scenario, the playlist needs a new song added at the end using the append() method. Choice B is correct because playlist.append('New Song') adds the new element to the end of the list, making it the sixth and final element. Choice D is incorrect because playlist[0] = 'New Song' would replace the first song rather than add a new one, which often occurs when students confuse assignment with addition. To help students: Demonstrate append() with physical objects being added to the end of a line, contrast append() with insert() and assignment operations, and use visual animations showing lists growing. Practice identifying when to modify existing elements versus adding new ones.

8

A to-do list of strings starts as 'pay bills','call mom','exercise','read','clean room'. You decide to prioritize by sorting alphabetically, like organizing sticky notes by title. The code runs tasks.sort() and keeps all items. No tasks are added or removed. The sorted list becomes 'call mom','clean room','exercise','pay bills','read'. In the context of the problem, what is the index of the element in the list after sorting?

Index of 'pay bills' is 0

Index of 'pay bills' is 3

Index of 'pay bills' is 4

Index of 'pay bills' is 2

Explanation

This question tests understanding of list operations in programming, specifically the sort() method and finding indices after sorting. In programming, lists are used to store collections of items. Common operations include adding, removing, and accessing elements using indices. In this scenario, the to-do list is sorted alphabetically, changing from ['pay bills','call mom','exercise','read','clean room'] to ['call mom','clean room','exercise','pay bills','read']. Choice C is correct because after sorting, 'pay bills' moves to index 3 (fourth position) in the alphabetically ordered list. Choice A is incorrect because it suggests index 2, which often occurs when students count the position of 'pay bills' in the original list or miscount in the sorted list. To help students: Practice sorting lists manually first, then trace where each element moves, create before-and-after index maps, and emphasize that sort() rearranges all elements. Use exercises where students predict new positions after sorting.

9

A sensor readings list of integers starts as 19, 21, 20, 22, 18. You receive a new reading and add it using readings.append(23) to keep the sequence. Based on the scenario above, how many elements are in the list after the given operations?

7 elements

5 elements

6 elements

Index 6 is accessed

Explanation

This question tests understanding of list operations in programming, specifically how append() affects list size. In programming, the append() method adds one element to the end of a list, increasing its length by exactly one. In this scenario, the sensor readings list [19, 21, 20, 22, 18] starts with 5 elements, and append(23) adds one more element. Choice B is correct because 5 + 1 = 6 elements after the append operation, with the new reading 23 added at the end. Choice A is incorrect because it shows the original count, indicating the student didn't account for the append operation's effect on list size. To help students: Practice counting elements before and after append operations, use visual representations showing list growth, and emphasize that each append() increases length by exactly one. Have students verify counts by listing all elements after operations.

10

A sensor readings list of integers starts as 21, 22, 20, 23, 22. A technician checks one specific reading using an index, like looking up a time stamp in a log. The code prints readings3 using zero-based index. No readings are modified or removed. The list stays in the same order. Based on the scenario above, what is the output after executing the following list operation?

20

21

22

23

Explanation

This question tests understanding of list operations in programming, specifically accessing elements by index. In programming, lists are used to store collections of items. Common operations include adding, removing, and accessing elements using indices. In this scenario, the sensor readings list [21, 22, 20, 23, 22] is accessed at index 3 using readings[3]. Choice C is correct because in zero-based indexing, index 3 refers to the fourth element, which is 23 (indices 0→21, 1→22, 2→20, 3→23, 4→22). Choice D is incorrect because it shows the value at index 0 (21), which often occurs when students think index 3 means 'third from the end' or miscount positions. To help students: Use visual number lines showing index positions below list elements, practice with finger counting starting from 0, and create index-to-value mapping exercises. Emphasize that the first element is always at index 0, not 1.

Page 1 of 2