Back to Student Worksheet
CS Grade 9-12 Answer Key

CS: Arrays and Lists

Storing, accessing, and updating ordered data

Answer Key
Name:
Date:
Score: / 12

CS: Arrays and Lists

Storing, accessing, and updating ordered data

CS - Grade 9-12

Instructions: Read each problem carefully. Show your work in the space provided. Assume indexes start at 0 unless the problem says otherwise.
  1. 1

    A list named scores stores the values [88, 92, 75, 100, 67]. What value is stored at index 2? Explain how you found it.

    Start counting positions at 0, not 1.

    The value at index 2 is 75. With 0-based indexing, index 0 is 88, index 1 is 92, and index 2 is 75.
  2. 2

    A list named colors starts as ["red", "blue", "green", "yellow"]. After the statement colors[1] = "purple", what is the list?

    The list becomes ["red", "purple", "green", "yellow"]. The value at index 1, which was "blue", is replaced by "purple".
  3. 3

    A program has the list nums = [4, 8, 15, 16, 23, 42]. Write the value of nums[0] + nums[5] and explain your answer.

    Use the first and last valid indexes in the list.

    The value is 46. nums[0] is 4 and nums[5] is 42, so 4 + 42 = 46.
  4. 4

    A list named names contains 6 elements. What are the valid index values for this list if the language uses 0-based indexing?

    The valid index values are 0, 1, 2, 3, 4, and 5. A list with 6 elements starts at index 0 and ends at index 5.
  5. 5

    Explain the difference between an array and a list in many programming languages. Include one example of when a list may be easier to use.

    Think about whether the number of items can change while the program runs.

    An array often has a fixed size and stores values in order, while a list often can grow or shrink as items are added or removed. A list may be easier to use when a program needs to keep adding new user names because the number of names is not known ahead of time.
  6. 6

    A list starts as [10, 20, 30]. A program appends 40 to the list. What is the list after the append operation?

    The list becomes [10, 20, 30, 40]. Appending adds the new value to the end of the list.
  7. 7

    A list named letters starts as ["a", "b", "c", "d"]. A program removes the element at index 2. What is the list after the removal?

    Find the item at index 2 before removing anything.

    The list becomes ["a", "b", "d"]. The element at index 2 is "c", so it is removed and the later elements shift left.
  8. 8

    Trace this loop and write the final value of total: nums = [3, 5, 2, 10]; total = 0; for each n in nums: total = total + n.

    The final value of total is 20. The loop adds 3, then 5, then 2, then 10, so total is 3 + 5 + 2 + 10 = 20.
  9. 9

    Write pseudocode that prints every element in the list temperatures = [72, 68, 75, 70].

    Use a loop so the same print step can run for each element.

    One correct answer is: for each temp in temperatures, print temp. This loop visits each element of the list and prints it one at a time.
  10. 10

    A list named values is [9, 1, 6, 3]. Write pseudocode to find the largest value in the list.

    One correct answer is: set largest to values[0]; for each value in values, if value is greater than largest, set largest to value; after the loop, largest is the greatest value. For this list, the largest value is 9.
  11. 11

    A program tries to access items[10] when items has length 10. Explain the error.

    The last valid index is one less than the length.

    This causes an index out of range error in a 0-based list. If the length is 10, the valid indexes are 0 through 9, so index 10 is not valid.
  12. 12

    A list of test scores is [80, 95, 70, 100, 85]. Write pseudocode to count how many scores are at least 90.

    Use a counter variable that increases only when a score meets the condition.

    One correct answer is: set count to 0; for each score in the list, if score is greater than or equal to 90, add 1 to count; after the loop, count is the number of scores at least 90. For this list, count is 2 because 95 and 100 are at least 90.
LivePhysics™.com CS - Grade 9-12 - Answer Key