15-112
Spring 2014 Quiz 9
* 20 minutes. No calculators, no notes, no books, no
computers. * SHOW YOUR WORK
Multiple Choice. [50 pts; 5 pts each] Clearly circle your answers.
For each of the following, you will answer
one of A-E, as such:
(A) Always True
(B) Usually True but may be False (provide reason or example)
(C) Usually False but may be True (provide reason or example)
(D) Always False
(E) None of the above
As noted, if you answer (B) or (C), you need to provide a reason or example for
the exceptional case.
Also, assume we have new-style classes A, B, and C, and that A and B extend
object, but C extends A. Also, assume a1, a2, etc, are instances of A,
b1, b2,É are instances of B, and c1, c2, É are instances of C, and that d1, d2,
É are instances but you do not know of which class.
Also, you should not assume that the classes are necessarily defined properly. For example, whilto have a __str__ function as such:
def __str__(self): return str(random.random())
So: say we are considering this possible fact (for any instance a1 of the class A):
str(a1) == str(a1)
This may seem to be always True, but what if the __str__ method in A is defined as such:
def __str__(self): return str(random.random())
This of course is e a terrible idea, and not generally seen, but it is possible in theory. And this strange __str__ method would in fact return different strings for the same instance. Hence, the possible fact is usually True, but may be False, and so the answer would be B.
1.
if (a1 == a2) then (str(a1) == str(a2))
2.
eval(str(a1)) == a1
3.
isinstance(c1, type(a1))
4.
if (isinstance(d1, B)) then (isinstance(d1, A))
5.
if method C.foo overrides A.foo, then calling c1.foo() will at
some point run the code defined in A.foo.
6.
For set s, if (a1 == a2), and (a1 in s), then (a2 in s).
7.
For set s, if (a1 is a2), and (a1 in s), then (a2 in s).
8.
For set s, if (a1 in s) and (a2 in s), then (a1 == a2)
9.
For set s, if (a1 in s), then (eval(repr(a1)) in s)
10. If (c1.x == 42), then (c1.__dict__[ÒxÓ] == 42)
Quick Answers: [40 pts; 10 pts each] Be very brief.
Code Tracing. Indicate what this will print. [10 pts; 1 pt per call to the function p defined below]
def p(f, *args):
try: return f(*args)
except: return
"ack"
def f(x,y): return x/y
print "A:", p(f,-2, -1),
p(f, -1, 0),\
p(f, 0, 1), p(f, 1, 2, 3)
def f(x): return lambda y: (x,y)
print "B:", p(f(3), range(3)), p(f(4), *range(4))
f = lambda L, x=3: [x*val for val in L]
print "C:", p(f, range(2,
6)),\
p(f, range(0, -3, -1), 2)
f = lambda g: (lambda x: g(x)*x)
def g(x): return x+3
print "D:", p(f(g),3),
p(f(f(g)), 3)
go()
Bonus/Optional: [5 pts] What will
the following print?
print [(lambda x:lambda
y:2*x+y)(y)(x) for x in range(3, 13, 4) for y in range(2, 4)]