Name:
Section: Date:
INSTRUCTIONS:
(1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.
(2) This exam booklet contains 26 questions, each of which will be weighted equally at 5 points each. It also contains two separate 10-point written questions. The total points for the exam is 150 points.
(3) You may use one 8.5” x 11” note sheet during the exam. No other reference materials or electronic devices may be used during the examination, i.e. no cal- culators, cell phones, mp3 players, etc.. Paper dictionaries are allowed.
(4) Questions will not be interpreted during the examination.
(5) You should choose the single best alternative for each question, even if you be- lieve that a question is ambiguous or contains a typographic error. If a question has more than one best answer, credit will be given for any of the correct answers.
Provide only one answer.
(6) Please fill in the requested information at the top of this exam booklet.
(7) Use a #2 pencil to encode answers on the OMR form.
(8) Please encode the following on the OMR form:
– Last name and first initial – MSU PID
– Exam form (2 A)
(9) Please sign the OMR form.
(10) Only answers recorded on your OMR form for multiple choice questions will be counted for credit. Completely erase any responses on the OMR form that you wish to delete.
(11) You must turn in this exam booklet, the OMR form, your note sheet, and your scrap paper when you have completed the exam. Be sure your name is on any of these items that you would like to have returned.
KEY
1 2 3 4 5 6 7 8 9 10 11 12 13
C A D C E D D C B A C A D
14 15 16 17 18 19 20 21 22 23 24 25 26
B D B B B A C C E C C B A
(1) Given the following code, what is printed?
L1 = [1, 2]
L2 = [4, 5]
L1.extend(L2) print(L1)
(a) [1, 2]
(b) [1, 2, L2]
(c) [1, 2, 4, 5]
(d) [1, 2, [4, 5]]
(e) None of the above.
(2) Given the following code, what is printed?
L1 = [1, 2]
L2 = [4, 5]
L1.extend(L2) print(L2)
(a) [4, 5]
(b) [L1, 4, 5]
(c) [L1, L2]
(d) [1, 2, 4, 5]
(e) None of the above.
(3) Given the following code, what is printed?
L1 = [1, 2]
L2 = [4, 5]
L1.append(L2) print(L1)
(a) [1, 2]
(b) [4, 5]
(c) [1, 2, 4, 5]
(d) [1, 2, [4, 5]]
(4) Given the following code, what is printed?
L1 = [1, 2]
L2 = [1, L1]
print(L2[0],L2[1],L2) (a) 1 L1 [1, L1]
(b) 1, L1, [1, L1]
(c) 1 [1, 2] [1, [1, 2]]
(d) 1, [1, 2], [1, [1, 2]]
(e) None of the above.
(5) Given the following code, what is printed?
L1 = [1, 2]
L2 = [1, L1]
L1[1] = 3
print (L2[0][0]) (a) 1
(b) [[1]]
(c) 2 (d) 3
(e) None of the above.
(6) Given the following code, what is printed?
L1 = [1, 2]
L2 = [1, L1]
L1[1] = 3
print (L2[1][1]) (a) 2
(b) [[2]]
(c) 1 (d) 3
(e) None of the above.
(7) Which of the following is (are) immutable?
(a) "hello"
(b) (1, 2, 3) (c) 5.78
(d) All of (a)–(c) are immutable.
(e) None of (a)–(c) are immutable.
(8) Following the assignment s = {i.lower() for i in ’Hullabaloo’}, what is returned by len(s)?
(a) 10 (b) 0
(c) 6
(d) An error.
(e) None of the above.
(9) Following the assignment s = {i.lower() for i in ’Hullabaloo’}, what is returned by ’H’ in s?
(a) True (b) False
(c) 1
(d) An error.
(e) None of the above.
import math
def my fun(n, m):
return (n * m)
print(my fun(math.sqrt(36),2)) # Line 1 print(my fun(3,"Ha")) # Line 2
Figure 1
(10) What is printed by Line 1 in Figure 1?
(a) 12.0 (b) 72.0
(c) 18
(d) An error.
(e) None of the above.
(11) What is printed by Line 2 in Figure 1?
(a) Ha3 (b) 3Ha
(c) HaHaHa (d) An error.
(e) None of the above.
d = {0:1}
x = 0 y = 0
for n in range(2,6):
if n//2 in d:
d[n//2] = d[n//2] + n else:
d[n//2] = n for v in d.values():
x = x + v for v in d:
y += v
print(d[2]) # Line 1 print(x) # Line 2 print(y) # Line 3
print(d[0] in d.items()) # Line 4
Figure 2 (12) What is printed by Line 1 in Figure 2?
(a) 9 (b) 5 (c) 4
(d) An error.
(e) None of the above.
(13) What is printed by Line 2 in Figure 2?
(a) 0 (b) 3 (c) 14 (d) 15
(e) None of the above.
(14) What is printed by Line 3 in Figure 2?
(a) 0 (b) 3 (c) 14 (d) 15
(15) What is printed by Line 4 in Figure 2?
(a) 0 (b) 1
(c) True (d) False
(e) None of the above.
# remember mutable vs. immutable!
def fun1 (lst1,num):
lst1[-1] = num return lst1 L1 = list(range(3)) L2 = fun1(L1,4)
print(L1) # Line 1
print(L2) # Line 2
def fun2 (s1,s2):
s1 = (s1[len(s1)-1], s1[0]) s2 = max(s2) + min(s2) return s1,s2
tuple1 = (’a’, ’b’, ’c’) tuple2 = (’A’, ’B’, ’C’) a,b = fun2(tuple1,tuple2)
print(a) # Line 3
print(b) # Line 4
print(tuple1,tuple2) # Line 5
Figure 3 (16) What is printed by Line 1 in Figure 3?
(a) [0, 1, 2]
(b) [0, 1, 4]
(c) [1, 2, 3]
(d) [1, 2, 3, 4]]
(e) None of the above.
(17) What is printed by Line 2 in Figure 3?
(a) [0, 1, 2]
(b) [0, 1, 4]
(c) [1, 2, 3]
(d) [1, 2, 3, 4]]
(e) None of the above.
(18) What is printed by Line 3 in Figure 3?
(a) (’c’, ’b’, ’a’) (b) (’c’, ’a’)
(c) ca (d) cba
(19) What is printed by Line 4 in Figure 3?
(a) CA (b) AC
(c) (’C’, ’A’) (d) CBA
(e) None of the above.
(20) What is printed by Line 5 in Figure 3?
(a) (’c’, ’a’, ’CA’)
(b) (’c’, ’b’, ’a’, ’C’, ’B’, ’A’) (c) (’a’, ’b’, ’c’) (’A’, ’B’, ’C’) (d) (’a’, ’b’, ’c’, ’A’, ’B’, ’C’)
(e) None of the above.
fobj = open("infile.txt","r") a dict = {}
n = 0
for l in fobj:
n += 1
l = l.strip() for c in l:
if c in a dict:
a dict[c].append(n) else:
a dict[c] = [n]
fobj.close() if ’o’ in a dict:
print(a dict[’o’]) # Line 1
if ’La’ in a dict:
print(a dict[’La’]) # Line 2
print(max([k.lower() for k in a dict])) # Line 3
Do Re Do Fa So La Ti Do
Figure 4
(21) What is printed by Line 1 in Figure 4 if the contents of infile.txt is as shown at top right?
(a) [3]
(b) [1, 2, 3]
(c) [1, 1, 2, 3]
(d) A blank line.
(e) None of the above.
(22) What is printed by Line 2 in Figure 4 if the contents of infile.txt is as shown at top right?
(a) [3]
(b) [1, 2, 3]
(c) [1, 1, 2, 3]
(d) A blank line.
(e) None of the above.
(23) What is printed by Line 3 in Figure 4 if the contents of infile.txt is as shown at top right?
(a) o (b) Ti
(c) t (d) do
# remember the Python scope rules!
def my fun(p):
a = p + 2 # Line 1
p = 2*a # Line 2
return (a + p + b) # Line 3 a = 20
b = 30
print(my fun(b)) # Line 4
print(a) # Line 5
Figure 5
(24) Which line in Figure 5 references a global variable?
(a) Line 1 (b) Line 2 (c) Line 3
(d) All of Line 1, Line 2, and Line 3.
(e) None of the above.
(25) What is printed by Line 4 in Figure 5?
(a) None (b) 126
(c) 80 (d) 110
(e) None of the above.
(26) What is printed by Line 5 in Figure 5?
(a) 20 (b) 30 (c) 32 (d) 64
(e) None of the above.
This page left intentionally blank.
Name AND Section:
Written #1: (10 points) Write correct Python code to meet the following specifications. Write all of your answer neatly on this sheet. Indicate inden- tation with care.
Write a Python function named get input file that a program can call with- out any arguments to get a reference to a file that is opened for input (read- ing). The function must prompt the user for the name of the file to open.
If the indicated file cannot be opened for input, the function must continue prompting the user for a different file name. The function must return an object that references the opened file.
Written #2: (10 points) Write correct Python code to meet the following specifications. Write all of your answer neatly on this sheet. Indicate inden- tation with care.
Write a Python function named truncate with two parameters: The first should indicate a required argument, which must reference a file that is al- ready open for input (reading). The second parameter should indicate an optional argument, which must be a non-negative integer n with a default value of 10. The truncate function should read the contents of the indicated file, printing out the first n characters of each line, or the entire line if n is bigger than the line length.
Note: Your truncate function should not re-open the file, or an error will occur when it is called with a proper argument.
To illustrate, the next page shows examples for a sample input file.
For example:
Assume the contents of the input file data.txt is as follows:
Hey diddle diddle, the cat and the fiddle.
The cow jumped over the moon.
The little dog laughed to see such sport and the dish ran away with the spoon.
Assume the main program is as follows:
f = open("data.txt", "r") truncate(f)
f.close()
print("******")
f = open("data.txt", "r") truncate(f, 20)
f.close()
This main program should print:
Hey diddle The cow ju The little and the di
******
Hey diddle diddle, t The cow jumped over The little dog laugh and the dish ran awa