Lists
Help Questions
AP Computer Science Principles › Lists
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.
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','Blue','Gold','Pulse','River']
['Blue','Gold','Neon','Pulse','River']
['Blue','Gold','Pulse','River','Neon']
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.
A sensor readings list of integers starts as 18, 19, 21, 20, 19. After transferring data, the system resets the list, like clearing a clipboard after pasting. The code runs readings.clear() to remove all elements. No new readings are added afterward. The list becomes empty. Based on the scenario above, how many elements are in the list after the given operations?
0
1
4
5
Explanation
This question tests understanding of list operations in programming, specifically the clear() method that removes all elements. 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 with 5 elements is completely emptied using the clear() method, like erasing all data from a clipboard. Choice C is correct because clear() removes all elements from the list, leaving it empty with 0 elements, regardless of how many items were originally present. Choice B is incorrect because it suggests the list still has 5 elements, which often occurs when students think clear() only resets values to zero rather than removing all elements entirely. To help students: Demonstrate clear() with physical containers being emptied completely, contrast with remove() which deletes single items, and show that an empty list has length 0. Practice checking list lengths after various operations.
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[2] = grades[2] + '10'
Change to grades[1] = grades[1] + 10
Change to grades.remove(1)
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.
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 4
Index of 'pay bills' is 0
Index of 'pay bills' is 3
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.
A shopping cart list of strings starts as 'soap','shampoo','toothpaste','towel','lotion'. The shopper wants to remove the last item by index, like putting the last-picked item back on the shelf. The code tries cart.remove(cart5) to remove that last element. The list has only five items with zero-based indices 0 to 4. Nothing else changes. Based on the scenario above, identify the error in the list operation and suggest a correction.
Use cart[5] = 'lotion' instead
Use cart.remove(cart[4]) instead
Use cart.append(cart[5]) instead
Use cart.remove(cart[5]) instead
Explanation
This question tests understanding of list operations in programming, specifically index bounds and the remove() method. 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 has 5 items (indices 0-4), but the code incorrectly tries cart.remove(cart[5]), which causes an index out of bounds error. Choice A is correct because cart.remove(cart[4]) properly accesses the last element (at index 4) in a 5-element list with zero-based indexing. Choice B is incorrect because it still uses index 5, which doesn't exist, showing students haven't understood the core issue of index bounds. To help students: Create index charts showing valid ranges for lists of different sizes, practice identifying the last valid index (always length - 1), and use error messages as learning opportunities. Emphasize checking list bounds before accessing elements.
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?
['eggs','milk','bread','apples','rice']
['milk','bread','apples','rice','eggs']
['milk','bread','eggs','apples','rice']
['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.
A to-do list of strings starts as 'email boss','wash dishes','study','buy groceries','walk dog'. You mark one task complete by removing it, like crossing a note off a fridge list. The code runs tasks.remove('wash dishes') to delete that task. The remaining tasks stay in their original order. No new tasks are added. Based on the scenario above, how many elements are in the list after the given operations?
3
4
5
6
Explanation
This question tests understanding of list operations in programming, specifically counting elements after a remove() operation. 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 starts with 5 tasks and one task ('wash dishes') is removed using the remove() method. Choice C is correct because after removing one element from a 5-element list, 4 elements remain (5 - 1 = 4), which is a fundamental counting operation. Choice D is incorrect because it suggests no change in the list size, which often occurs when students think remove() only marks items rather than actually deleting them. To help students: Use visual diagrams showing lists shrinking when elements are removed, practice counting operations with physical objects, and emphasize that remove() permanently deletes elements. Encourage students to verify list lengths before and after operations using len() function.
A student grades list of integers starts as 78, 92, 85, 69, 88. After a re-check, the third grade increases by 5 points, like correcting a score on a paper. The code updates grades2 = grades2 + 5 using zero-based index. All other grades stay the same. The list order does not change. In the context of the problem, what is the output after executing the following list operation?
[78, 92, 85, 69, 93]
[78, 92, 85, 74, 88]
[78, 97, 85, 69, 88]
[78, 92, 90, 69, 88]
Explanation
This question tests understanding of list operations in programming, specifically modifying elements by index and performing arithmetic operations. In programming, lists are used to store collections of items. Common operations include adding, removing, and accessing elements using indices. In this scenario, the grades list [78, 92, 85, 69, 88] has its third element (index 2, value 85) increased by 5 points using grades[2] = grades[2] + 5. Choice A is correct because the operation changes only the element at index 2 from 85 to 90 (85 + 5 = 90), resulting in [78, 92, 90, 69, 88]. Choice D is incorrect because it shows the second element (index 1) changed instead, which often occurs when students confuse one-based counting with zero-based indexing. To help students: Create index reference cards showing positions 0-4 for a 5-element list, practice tracing operations with index arrows, and emphasize that list indices start at 0. Use debugging exercises where students predict which element will change.
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['New Song'].append()
playlist[0] = 'New Song'
playlist.append('New Song')
playlist.remove('New Song')
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.