CPSC120B
Fundamentals of Computer Science I

Review 3

A practice test to review for the third test.

  1. For all of the data types we have covered this semester, list them as either mutable or immutable.

  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. a_string = "0123456789"
      for index in range(0, len(a_string), 2):
          print(a_string[index])
      
    2. a_string = "abcd"
      for i in range(len(a_string)):
          print(a_string + a_string)
      
    3. a_string = "abcd"
      print(a_string[2:4] + a_string[0:2])
      
    4. a_string = "my string!"
      while(a_string[0] != ' '):
          a_string = a_string[1:]
      print(a_string)
      
    5. a_list = [0, 1, 2, 3]
      b_list = []
      index = 0
      while index <= len(a_list):
          b_list.append(a_list[index])
          index = index + 1
      print(b_list.reverse())
      
    6. def spam(eggs, ham):
          eggs = eggs + ham
      
      eggs = "hello"
      ham = "world"
      spam(eggs, ham)
      print(eggs)
      
    7. def spam(eggs, ham):
          eggs.append(ham)
      
      eggs = ["hello"]
      ham = "world"
      spam(eggs, ham)
      print(eggs)