CPSC120A
Fundamentals of Computer Science I

Activity 21

Review for Test 2.

  1. What is the difference between a method and a function?

  2. What is the negation of the Python expression a < 0 and b > 0?

  3. 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 character in a_string:
          print(a_string + a_string)
      
    3. x = 7
      y = x
      
      print(not(x > y and y < x))
      
    4. a_string = "abcd"
      print(a_string[2:4] + a_string[0:2])
      
    5. a_string = "my string!"
      while(a_string[0] != ' '):
          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] + b_string
          index = index + 1
      print(b_string)
      
  4. What is the decimal number -77 in binary?

  5. Write a function called count_uppercase(my_string) which takes as a parameter a string of characters. It should return an integer, the number of uppercase letters in my_string. Include your own test cases.