CIT 591 Midterm Exam, Fall 2011 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. If the question asks for a statement, write a statement, not a function. If the question asks for a function, write a function, not a class.

1. (5 points) The variable `score` is supposed to be between 0 and 100, inclusive. Write a statement or statements (not a method) to set `score` to zero if it is negative, or to `100` if it is larger than 100.

2. (5 points) Suppose `aList` is a list of integers.Write a statement or statements (not a method) to print out the first negative number in `aList`. If there are no negative values, don't print anything.

3. (5 points) Write a function that, given a list of integers, returns the first negative number in the list. If there are no negative numbers, it should return `None`.

4. (5 points) Use a list comprehension to set `powers` to a list containing the first 100 powers of 2 (that is, `[1, 2, 4, 8, 16, 32, ...]`).

5. (30 points) Tell what each of the following functions does. That is, assuming each function is called with the correct type of parameters, tell what is computed and returned. Also, if there are side effects, tell what they are. Do not provide a line by line description of how the function does its job, just tell what it accomplishes.

 a. ```def firstBad(aList): # aList is a list of integers for i in range(0, len(aList)): if aList[i] < 0 or aList[i] > 100: return i return -1 ``` b. ```def buzz(anInt): # anInt is a positive integer if anInt % 7 == 0 or '7' in str(anInt): return 'buzz' else: return anInt ``` c. ```def flip(aList): # aList is a list of numbers x = aList[:] for i in range(1, len(aList)): if x[i] < x[i - 1]: x[i], x[i - 1] = x[i - 1], x[i] return x[-1] ``` d. ```def mapEven(aList): # aList is a list of positive integers even = map(lambda x: x % 2 == 0 return list(even, aList)) ``` e. ```def filterEven(aList): # aList is a list of positive integers f = lambda x: x % 2 == 0 return list(filter(f, aList)) ``` f. ```import functools def reduceRunning(aList): # aList is a list of floating point numbers f = lambda x, y: (x + y) / 2 return functools.reduce(f, aList) ``` (Tell generally what this function does; you don't have to be numerically exact.)

6. (5 points) In pair programming, what are the names of the two roles, and what should the person who is not typing be doing?

7. (5 points) Mark each statement about Test Driven Development (TDD) as True or False.

1. ________ TDD guarantees that your program will be correct.

2. ________ TDD encourages the use of short, single-purpose functions.

3. ________ TDD reduces the amount of time spent debugging.

4. ________ TDD results in more efficient programs.

5. ________ TDD makes programs easier to modify at some future date.

8. (5 points) A function named `largest` takes a list of integers as a parameter, and returns the largest number in the list (or `None` if the list is empty). Write a unit test method to test `largest`.

9. (5 points) Tell what you would need to change in the answer to the previous question if `largest` takes a list of floating point numbers instead of integers. (You can rewrite the entire method if that's easiest, or just tell what changes need to be made.)

10. (5 points) The following recursive function is supposed to compute the factorial of a positive number (for example, factorial(5) = 5*4*3*2*1). It doesn't work. Fix it (do not rewrite the function to use a loop, just fix the error.)
```def factorial(n):
return n * factorial(n - 1)
```
11. (5 points) An Ace is worth 4 points; a King is worth 3 points; a Queen is worth 2 points; and a Jack is worth 1 point. Create a Python dictionary named `cardValues` to hold this information.

12. (5 points) Inside a function `foo` you see the statement `print(bar)`. How do you figure out whether `bar` is local or global? Give a rule or rules for determining this.

13. (5 points) The list `testScores` contains integers in the range 0 to 100; but since some people didn't take the test, the list may also contain some `None` values. Write a statement or statements (not a complete method) to remove those `None` values from the list. For full credit, do this in the fewest possible number of statements.

14. (5 points) In a method inside a class, what important value is held in the variable `self`? Or, to put the question another way, what is special about this particular value that isn't true about any other variable in the method?

15. (5 points) There are rules for good programming style, and they may help, but good style isn't about following a bunch of rules. According to your instructor, what is the purpose of those rules? What are they intended to help you achieve?