CIT 590 Midterm Exam, Fall 2016 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. (10 points) The following functions are all intended to check whether a string contains any lowercase letters, but at least some of them are wrong. For each function, describe what the function actually does (assuming that the parameter is a string).

 def any_lowercase1(s): for c in s: if c.islower(): return True else: return False Result is based only on first letter of string s def any_lowercase2(s): for 'c' in s: if c.islower(): return 'True' else: return 'False' SyntaxError: can't assign to literal def any_lowercase3(s): for c in s: flag = c.islower() return flag Result is based only on last letter of string s def any_lowercase4(s): flag = False for c in s: flag = flag or c.islower() return flag Works correctly. def any_lowercase5(s): for c in s: if not c.islower(): return False return True Return True only if all letters in s are lowercase.

2. (12 points) In this question you will construct a function to make the computer's move in a game of Pig. Follow the instructions, and write one single statement for each instruction. Any time you put more than one statement in a box, it will be marked wrong. Show indentation.
 Write the header for a method named computer_move that takes no parameters. def computer_move(): Write a statement to print "Computer's turn", preceded by a blank line print("\nComputer's turn") Set an integer variable named sum to zero. sum = 0 Start a loop that will run until sum is 17 or larger. while sum < 17: Set a variable roll to a random integer between 1 and 6, inclusive. (Assume random has been imported.) roll = random.randint(1, 6) Print out a message saying what the computer has just rolled. print("The computer rolls", roll) Test if the number rolled is a 1 . . . if roll == 1: And if it is, return a zero. return 0 . . . but if it isn't a 1 . . . else: Add the number rolled to sum. sum += roll // OR  sum = sum + roll After the loop is finished, print out a message saying how much the computer has gained this turn. print("The computer gets", sum) Return from the function with the amount gained this turn. return sum

3. (3 points)What words do "REPL" stand for?  Read-Eval-Print-Loop

4. (3 points) What words do "TDD" stand for?  Test Driven Development (also ok: Test Driven Design)

5. (3 points) Python 2 used ASCII to represent characters. What does Python 3 use? Unicode (also okay: UTF-8)

6. (3 points) What operation is represented by the ^ operator?  Bitwise exclusive or (also ok: XOR or EOR)
This is also the set operator "symmetric difference" (in either set but not both), so that is also an acceptable answer.

7. (3 points) When we talk about a "unit test," what is the "unit" being tested?  A single method (or: a single function)

8. (3 points) What keystroke in IDLE loads your program into the shell window?  F5

9. (10 points) Suppose abc = "abcdefg"

1. What is abc[2:4] ?   'cd'

2. What is abc[5:] ?    'fg'

3. What is abc[:5] ?   'abcde'

4. What is abc[-3 : -1]   'ef'

5. What is abc * 3  'abcdefgabcdefgabcdefg'

10. (5 points) Suppose cards = {"ace" : 1, "deuce" : 2, "trey" : 3}
What does the following print?
for i in cards:
print(i)
ace
deuce
trey

11. (5 points) Write an anonymous ("lambda") function that takes one integer argument and returns twice that number.

lambda x: 2 * x    or    lambda x: x + x
(Okay if the student assigns this to a variable)

12. (5 points) Write an anonymous ("lambda") function that takes one integer argument and returns half that number if it is even, but three times that number if it is odd.

lambda x: x // 2 if x % 2 == 0 else 3 * x
(Okay if the student assigns this to a variable)

13. (5 points) Write a statement that uses the format method to print out a floating point number x with two digits after the decimal point.
print('{:.2f}'.format(x))
Okay variations: e or g instead of f, some number before the decimal: {7.2f}, {:.2e}, {7.2g}, etc.

14. (6 points) Write three lines of code (one line per box) to do the following:
 Open a file named data.txt for reading. stream = open("data.txt", "r") Read in the entire file as a list of lines. lines = stream.readlines() Close the file. stream.close()

15. (5 points) Suppose:
a = [1, 2, [3, 4]]
b = a[:]
a[2] = [5, 6]

What is the value of b?
[1, 2, [3, 4]]

16. (5 points) Write a single statement to set squares to a list of the squares of the numbers 1 through 1000.

squares = [x * x for x in range(1, 1001)]

17. (6 points) Suppose small is defined as:
def small(a, b=5):
return a < b

What is the result of:
1. list(map(small, [1, 2, 3, 4, 5]))
[True, True, True, True, False]

2. list(filter(small, [1, 2, 3, 4, 5]))
[1, 2, 3, 4]

3. reduce(small, [1, 2, 3, 4, 5])
True   (give one point for [True])

18. (5 points) Write one line of  Tkinter code to create a button that (1) shows the text "Click me" and (2) when clicked, calls the function clicked().

Button(frame, text="Click me", command=clicked)   (the "frame" can be any name)

19. (3 points) Name (don't describe, just name) the three methods that can be used to arrange widgets in a Tkinter window.

pack, grid, place