CPSC120A
Fundamentals of Computer Science I

Review 3

A practice test to review for the third test.

  1. Give an example of a number that can not be represented exactly using a Python float value.

  2. Rewrite the following Python expression with a functionally equivalent expression that doesn't use the not operator.

        not (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 = "abcd"
      for character in a_string:
          print(a_string + a_string)
      
    2. a_string = "abcd"
      print(a_string[2:4] + a_string[0:2])
      
    3. accumulator = 0
      MAX_VALUE   = 10
      while(accumulator < MAX_VALUE):
          accumulator = 2 * (accumulator + 1)
      print(accumulator)
      
    4. 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 61 in binary?

  5. What is the binary number 10.01 in decimal?

  6. 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.

  7. Write a function called print_initials(my_string) which takes as a parameter a string of characters of the form "name1 name2". Your function should return a string of length 2, which is the concatenation of the first character of each name in the string.