CIT 591 Midterm Exam, Fall 2009 Name ______________________________

Please keep all answers short and to the point. Do not add information that is not asked for; you will lose points for getting it wrong.

  1. (2 points) In IDLE, what key or key sequence is used to run a program?


  2. (2 points) In IDLE, what key or key sequence is used to stop a runaway (infinite loop) program?


  3. (24 points) For each of the following, write a single (that is, one) Python statement:

    1. Tell the user to Enter your name, then read in, and assign to a variable, a string typed in by the user.



    2. Tell the user to Enter your age, then read in, and assign to a variable, an integer typed in by the user.



    3. Print out, on separate lines, Your name is var1 and Your age is var2, where var1 and var2 are the values of the two variables entered above.



    4. Print out the first character (only) in the string variable answer.



    5. Print out the last value (only) in the list variable classes.



    6. Create, and save in a variable, a list of all the characters in the string variable sentence.



    7. Create, and save in a variable, a new list containing all but the first and last values in the list variable scores.



    8. Create, and save in variable list2, a copy of the list variable list1.




    9. Set the variable odd to True if the integer variable count is an odd number, and False otherwise.



    10. Set the variables sum and product to the sum of x and y and the product of x and y, respectively.



    11. Set the variable mid to the "middle" value (neither the max nor the min) of the three integer variables x, y, and z.



    12. List variable list1 is a list of integers. Set variable list2 to contain all the positive integers in list1.



  4. (20 points) For each of the following, write the fewest possible Python statements to accomplish the required task. (Half credit if your answer is correct, but longer than necessary.)

    1. Use a for loop to print the contents of list variable list, in order, one value per line.




    2. Use a while loop to print the contents of list variable list, in order, one value per line.






    3. Use a for loop to print the contents of list variable list, in reverse order, one value per line.





    4. Print one of the words negative, zero, or positive, according to whether variable x is less than zero, zero, or greater than zero, respectively.









    5. Create a file named foo.txt, and write all the values in list variable words to it, one value per line.









    6. Write a function named isEven that, given a single integer parameter, returns True if the parameter is an even number, False otherwise.







    7. You are inside a test class. Write a unit test method that says calling collatz(7) should return 22.








    8. You are inside a test class. Write a unit test method that says calling evenRand() should return an even number.








    9. You are inside a test class. Write a unit test method that calls randBuzz(). The test should pass if randBuzz() returns either a positive number or the string 'buzz', and fail otherwise.










    10. Create, and save in a variable, a 10x10 array (list of lists), all of whose values are None.












  5. (6 points) What gets printed by each of the following?
    1. list1 = [1, 2, [3, 4]]
      list2 = list1[:] 
      list1[0] = 5
      list2[2][0] = 6
      print list1, list2
           

    2. list = [1, 2, 3]
      list = list.append('3')
      print list
      


    3. a = [1,2,3]
      b = [1,2,3]
      print a == b, a is b
      

  6. (4 points) State, in words, what the following function does. Be precise enough that someone could use the function correctly.
        def mystery(list):
            if len(list) == 1: return list[0]
            else: return mystery(list[1:])
    
    
    


  7. (4 points) Suppose you have a class Robot(object), and you want to create a class to represent a dog robot. What is the difference between saying class Dogbot(object) and saying class Dogbot(Robot) ?





  8. (18 points) Give a short definition for each of the following:

    1. recursion





    2. side effect





    3. dynamic programming





    4. magic number





    5. encapsulation





    6. refactoring





  9. (3 points) What kind of value may not be used as a key in a dictionary? What is the reason for this restriction?







  10. (2 points) In pair programming, what are the two roles called?



  11. (4 points) The DRY (Don't Repeat Yourself) principle applies to both code and data.

    1. Why is it a good idea not to copy and paste code, or otherwise duplicate code? That is, what problems result from having duplicate code?







    2. Why is it a good idea not to have the same information represented in more than one way? That is, what problems arise?







  12. (3 points) You have a class Person, with instance variables name and age. If child is an instance of person, it is legal to say child.age += 1, but this is poor practice. What priciple does this violate?






  13. (8 points) Besides having fewer bugs, programs that have been developed using TDD will generally be of higher quality. List four ways in which methods are likely to be better.