CPSC120A
Fundamentals of Computer Science I

Review for Final Exam

  1. What is the difference between mutable and immutable data types? What are some examples of each?

  2. For each of the following snippets of Python code, give what would be printed to the command line if run. If the snippet will not print anything because of an error, just put error.

    1. spam = [1]
      for i in range(3):
          spam.append(spam[i] + spam[i])
      print(spam[-1])
              
    2. spam = {1: 3, 2: 4, 3: 2, 4: 1}
      print(spam[spam[1]])
              
    3. spam = [[-1, 0, 1], [0, 1, 2], [1, 2, 3]]
      eggs = 0
      for i in range(3):
          eggs += spam[i][i]
      print(eggs)
              
    4. def spam(eggs):
          eggs = eggs + 2
      ham = 1
      spam(ham)
      print(ham)
              
    5. def spam(eggs):
          eggs.append(3)
      ham = [1, 2]
      spam(ham)
      print(sum(ham))