15-110 Spring 2011 Midterm 1
80 minutes
1.
[10
pts; 1 pt each] True or False (circle one):
a. TRUE or FALSE: Any binary number that ends in a 1 must be odd.
b. TRUE or FALSE: Any binary number that contains exactly one 1 must be a power of 2.
c. TRUE or FALSE: ASCII is a code for converting text, images, sounds, and other media to integers.
d. TRUE or FALSE: Selection sort is one of the fastest sorting algorithms.
e. TRUE or FALSE: A Turing Test uses a human judge, but a Reverse Turing Test does not.
f. TRUE or FALSE: eTeRNA uses crowd-sourcing to sequence DNA.
g. TRUE or FALSE: Short-circuit evaluation refers to return statements that occur prior to the last line of a function.
h. TRUE or FALSE: We read about a case where modeling a law in a programming language and then translating it back into law led to the passage of a streamlined version of that law.
i. TRUE or FALSE: Ex-President Mubarak of Egypt promoted the use of social media which, ironically, helped lead to his downfall.
j. TRUE or FALSE: Top-down design helps identify appropriate helper functions for solving a given problem.
2. [10 pts; 2 pts each] Very Short Answers:
Answer each of the following with only one or two words or numbers. Longer answers will be incorrect.
a. Partway
through sorting a list of random numbers, you notice that the first half of the
list is sorted, and the second half of the list is also sorted. What sorting algorithm do you suppose is
running?
b. A
truth table for a boolean function has 32 rows in it. How many variables does the function take?
c. Name
the Google tool that allows users to study how word frequencies evolve over
time.
d. Some
RGB values use one byte, or 8 bits, to represent each red, green, and blue
component. What is the largest unsigned
value that can be represented in 8 bits?
e. What property must a list have so that binary search will work properly on it?
3.
[10
pts; 1 pt each] Indicate what each will print:
Statement: |
Prints: |
print "Spring Break is almost
here!"[1:3] |
|
print 5/2 + 5/2*1.0 + 5.0/2 |
|
print "3**2", 3**2 |
|
print 5*3, str(5)*3 |
|
print len(str(float(2))) |
|
print 1234%10, 1234/10 |
|
print len([[2,3,4],[5,6,7]]) |
|
print
range(abs(min(1,max(-4,5),-3,2))) |
|
print [5*2]*2 |
|
print ((2 <= 3) or (6/0 > 8))
and ("a" <= "q") |
4.
[20
pts; 4 pts each] Indicate what each will print:
x = 5 |
x = 5 |
x = 2 |
#4 continued. Indicate what each will print:
def
f(s): |
def f(s, x,
y): |
5. [20 pts; 4 pts each] Reasoning about code
In these exercises, you should provide the arguments that cause the function to
return the specified value. In most
cases, there are many valid answers. You
only need to provide one of them.
def
f(a): |
def
f(x, y): |
def f(a): |
#5 continued. Reasoning About Code
In these exercises, you should provide the arguments that cause the
function to return the specified value.
In most cases, there are many valid answers. You only need to provide one of them.
def
f(s, t): |
def f(x): |
6. [10 pts]
Free Response: hasNoDigits
Write the function hasNoDigits(s), which takes a string s and returns True
if s does not contain any digits and False otherwise. For example, hasNoDigits(“abc”) returns True
but hasNoDigits(“a1”) returns False. You
are not responsible for malformed input.
7. [20 pts]
Free Response: hasMagicDiagonals
Write the function hasMagicDiagonals(a), which takes a 2d list and returns
True if the list has magic diagonals, and False otherwise. We will say that a 2d list has magic diagonals
(an invented term) if the list is square (same number of rows as columns) and
if the sums of the values along each diagonal are equal to each other and that
sum also equals the sum of the values in the first row. You are not responsible for malformed input.
Continued on next page
8. Bonus/Optional
a. Bonus/Optional: [2.5
pts] What is a better name for this
function?
def
f(a):
b = []
for c in range(len(a[0])):
b += [[]]
for r in range(len(a)):
b[c] +=
[a[r][c]]
return b
b. Bonus/Optional: [2.5
pts] What will this print?
def
f(x,y,z):
if (x+y+z < 3):
result = 1
print "base:",result
elif (y % (abs(x)+1) > z %
(abs(y)+1)):
result = f(y-1,x-1,z/2) +
f(z-1,x/2,y-2)
print "elif:", result
else:
result = 1 + f(x/2, y-3, z/4)
print "else:", result
return result
print f(7,5,5)