CIT 591 Midterm Exam, Fall 2010 Name ______________________________

Please keep all answers short and to the point. Do not add information that is not asked for; you will not gain points, but you may lose points if you get it wrong

  1. (35 points) Provide a brief explanation of the purpose and result of each function, for someone that might wish to call the function. That is, tell what the functon is going to return, given legal arguments, and mention any side effects it may have. Do not provide a statement-by-statement description of how the function does its job.

    def f1(ints): # ints is a list of integers n = 0 for i in range(1, len(ints)): if ints[i] > ints[n]: n = i return n
    def f2(ints): # ints is a list of integers for i in range(1, len(ints)): if ints[i] < ints[i - 1]: return False return True
    def f3(s): # s is a string count = 0 for i in range(1, len(s)): if s[i] == s[i - 1]: count += 1 return count
    def f4(ints): # ints is a list of integers flag = False for i in range(0, len(ints)): flag = flag or (ints[i] < 100) return flag
    def f5(s): # s is a string containing words and spaces count = 0 if s[0].isalpha(): count = 1 for i in range(1, len(s)): if s[i - 1] == " " and s[i].isalpha(): count += 1 return count
    def f6(n): # n is a positive integer k = 1 while n * k <= 100: k += 1 return n * k
    def f7(s): # s is a string global index for index in range(0, len(s)): return s[index]

  2. (10 points) If s is a string, then s[i].alpha() tests whether the character at location i in the string contains a letter.

    1. Write a function to test whether a given string contains any letters, and return True if there is at least one letter in the string. The first line has been written for you.

                def contains_some_letters(s):








    2. Write a function to test whether a given string consists entirely of letters, and return False if there any non-letters in the string. The first line has been written for you.

                def contains_only_letters(s):







  3. (15 points) Briefly give one reason for each of the following rules. If you can think of several reasons, give one that you think is important.

    1. Unit tests should not request input from the user.





    2. A function should not compute something and print its result.





    3. Tests should be written before the code that will be tested.





  4. (10 points) List five methods, with their parameters, that are defined in Python's unittest module. (Just list them, don't tell what they do.)















  5. (10 points) Short answer:

    1. When must you use the global statement inside a function? (Be precise.)





    2. What is the purpose of the __init__ method?




    3. What is the purpose of the __str__ method?





    4. What does it mean to say that a function is recursive?






    5. What words does the acronym TDD stand for?





  6. The following function does not work as intended:
    def my_reverse(lst):
        """Makes a copy of the lst, but in reverse order."""
        copy = lst
        for i in range(0, len(lst)):
            copy[i] = lst[len(lst) - i]
        return copy
    
    1. (5 points) What will be returned by the call my_reverse([1, 2, 3, 4, 5])?



    2. (5 points) What do you need to do to correct the method?




  7. (5 points) A nickname for "Robert" is "Bob"; a nickname for "William" is "Bill"; a nickname for "Cynthia" is "Cindy". Define a map named nickname to make it easy to look up these nicknames.





  8. (5 points) The use of global variables makes it more difficult to debug a program. Tell why this is so.