15-112 Fall 2011 Quiz 1: Version A
* 15 Minutes. No calculators, no notes, no books, no computers.
1. [30 pts; 2 pts each] Indicate what each statement will print:
Statement: |
Prints: |
print 13|11 |
|
print 13&11 |
|
print 13^11 |
|
print (~13)&11 |
|
print 25<<2, 25>>2 |
|
print 3**2/2, 3**2.0/2 |
|
print 13/4, 13%4 |
|
print 10, 0x10, 0b10 |
|
print float(10)/4 |
|
print float(10/4) |
|
print int(10.0/4) |
|
print round(10.0/4) |
|
print ((True and 5) or (5 > 0)) |
|
print False * 5 |
|
print type((2**576) / (2**575)) |
|
2.
[20 pts] Apply
DeMorgan’s Law to the expression
(x and (not y)) to
obtain an equivalent expression.
3.
[20 pts] For the following function f, find int values for x and y such
that f(x,y) will return True:
def f(x, y):
return (x>y>0) and (x+y == 8) and (x/y == (x%y - 1))
4.
[30 pts] On the back
of this sheet, write the function isPal, which takes an integer (which you
should assume is between 100 and 999 inclusive) and returns True if that integer
is a palindrome (the same forwards as backwards). Hint: this is the
same as verifying that its hundreds digit matches its ones digit.
5.
Bonus/Optional [5
pts]: What will this print?:
print
(((0b11011)&(0x11011))>>3<<3)**2/7//8%9
def f(x): return 5 if
(x < 7) else x+f(x/2)
print f(35)
15-112 Fall 2011 Quiz 1: Version B
* 15 Minutes. No calculators, no notes, no books, no computers.
1. [30 pts; 2 pts each] Indicate what each statement will print:
Statement: |
Prints: |
print 14|11 |
|
print 14&11 |
|
print 14^11 |
|
print (~14)&11 |
|
print 25<<2, 25>>2 |
|
print 3**2.0/2, 3**2/2 |
|
print 14/4, 14%4 |
|
print 0x10, 10, 0b10 |
|
print float(6)/4 |
|
print float(6/4) |
|
print int(6.0/4) |
|
print round(6.0/4) |
|
print ((True and 6) or (6 > 0)) |
|
print False * 6 |
|
print type((2**476) / (2**475)) |
|
2.
[20 pts] Apply
DeMorgan’s Law to the expression
((not x) and y) to
obtain an equivalent expression.
3.
[20 pts] For the following function f, find int values for x and y such
that f(x,y) will return True:
def f(x, y):
return (x>y>0) and (x+y == 9) and (x/y == 3*(x%y))
4.
[30 pts] On the back
of this sheet, write the function isPal, which takes an integer (which you
should assume is between 100 and 999 inclusive) and returns True if that integer
is a palindrome (the same forwards as backwards). Hint: this is the
same as verifying that its hundreds digit matches its ones digit.
5.
Bonus/Optional [5
pts]: What will this print?:
print
(((0b11011)&(0x11011))>>3<<3)**2/6//7%8
def f(x): return 5 if
(x < 7) else x+f(x/2)
print f(38)
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem