CSE160 Midterm 18sp
CSE160 Midterm 18sp
Instructions: This exam is closed book, closed notes. You have 50 minutes to complete it. It contains 10
questions and 9 pages (including this one), totaling 90 points. Before you start, please check your copy to
make sure it is complete. Turn in all 9 pages of the exam, together, when you are finished. When time has
been called you must put down your pencil and stop writing. A syntax sheet will be provided separately.
Points will be deducted from your score if you are writing after time has been called. You should only
use parts of Python that have been covered in the class so far.
1 6
2 4
3 6
4 6
5 12
6 8
7 8
8 12
9 14
10 14
Total 90
1
1) [ 6 pts] For each of the if statements below, write the output when x = 20, x = 40, and x = 100
in the table below. If there is no output then write “NO OUTPUT”.
a)
if x < 30:
print "line 1"
if x <= 40:
print "line 2"
elif x < 100:
print "line 3"
b)
if x > 20:
print "line 1"
else:
if x < 50:
print "line 2"
print "line 3"
x = 20 x = 40 x = 100
Code a)
Code b)
2) [4 pts] Write the output of the code below in the box here:
sum = 0 MY ANSWER:
for x in range(6, 0, -2):
for y in range(x):
sum = sum + y
print 'sum:', sum
2
3) [6 pts] What output is produced after running the following piece of code?
A = [1, 3, 7]
B = A
C = A[:]
A.append(C[-1])
B[2] = 5
C[1:2] = [9, 10, 11]
print A
print B
print C
MY ANSWER:
4) [6 pts] What output is produced after running the following piece of code?
def some_key(x):
return x[1] + x[2]
MY ANSWER:
3
5) [12 pts] For each of the following statements, show what is printed. If nothing is printed then
write “NO OUTPUT”.
x = -200
def fig(x):
if x < 100:
return "small"
else:
return "large"
def pear(x):
print "pear:", x
return x + 7
def apple(y):
y = pear(y)
print "apple:", y
a) print pear(10)
b) print apple(2)
c) print fig(120)
d) print fig(pear(2))
4
6) [8 pts] Given the following dictionary, write what each expression evaluates to. If an error is
thrown, write “Error”.
a) my_dict[2]
b) my_dict[3]
c) my_dict[1][1]
d) my_dict["red"]
7) [8 pts] What is the output of the following code? If the code has an error write “Error”.
a = {1, 4, 5, 6, 9}
b = {4, 6, 8, 9}
a) print b & a
b) print a | b
c) print b - a
d) print a.remove(8)
5
8) [12 pts] a) Draw the entire environment, including all active environment frames and all user-
defined values, at the moment that the MINUS OPERATION IS performed. Feel free to draw out
the entire environment, but be sure to CLEARLY indicate what will exist at the moment the
MINUS operation is performed (e.g. cross out frames that no longer exist).
b) When finished executing, what is printed out by this code? MY ANSWER:
c) How many different stack frames (environment frames) are active when the call stack is
DEEPEST/LARGEST? (Hint: The global frame counts as one frame.) MY ANSWER:
________________________________________________________________
x = 100
y = 200
def zebra(y):
return y + 2
def lion(x):
temp = zebra(zebra(x)) + 8
return zebra(x) - temp
def rhino(y):
temp = zebra(y) + 3
return lion(y) + temp
print rhino(x)
MY ANSWER:
6
9) [14 pts] Write a function get_youngest_person that takes a list of dictionaries as
arguments and returns the name of the youngest person in the list. The list of dictionaries will
have the following format:
people= [
{"name": "Alice", "age": 20},
{"name": "Bob", "age": 9},
{"name": "Dan", "age": 56}
]
def get_youngest_person(people):
# Write your code here
7
10) [14 points] Write a function called transpose that takes a pixel_grid as described in
Homework 3 as an argument and returns the transpose of that pixel_grid. This is identical
to how we would transpose a matrix: swap the rows and columns. For example, if we had:
The call: transpose(grid1) would return: [ [1, 4], A grid with 3 rows
[2, 5], and 2 columns
[3, 6] ]
You may assume that the provided pixel_grid contains at least one row and one column.
8
10) (continued)
def transpose(pixel_grid):
# Write your code here