CPSC120A
Fundamentals of Computer Science I

Extra Review 3

Extra tracing problmes to review for the third test.

  1. 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 = "abcd"
      a_index = a_string.find("b")
      print(a_string[a_index:] + a_string[:a_index])
      
    2. a_string = "abcd"
      for character in a_string:
          print(chr(ord(character) + 1))
      
    3. a_string = "abcd"
      a_length = len(a_string)
      for i in range(a_length):
          print(a_string[i] + a_string[a_length - i - 1])
      
    4. accumulator = 1
      while(accumulator < 10):
          accumulator *= 2
          print(accumulator)
      
    5. a_string = "my string!"
      while(len(a_string) > 3):
          a_string = a_string[:-1]
      print(a_string)
      
    6. a_string = "abcd"
      b_string = ""
      index = 0
      while index <= len(a_string):
          b_string = a_string[index] + '-'
          index = index + 1
      print(b_string)