printable version
Quiz 2 Review
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
Problem Q2r.1.
Show your intermediate work on each of the following.
- What base-10 number does the eight-bit two's-complement
bit pattern 01010101 represent?
- What base-10 number does the eight-bit two's-complement
bit pattern 10101010 represent?
- Convert 26(10) to an eight-bit two's-complement
representation.
- Convert −104(10) to an eight-bit two's-complement
representation.
| a. | 85(10) |
| b. | −86(10) |
| c. | 00011010 |
| d. | 10011000 |
Problem Q2r.2.
Perform each of the following conversions.
| a. |
100101(2) to decimal |
| b. |
53(10) to binary |
| c. |
the eight-bit two's-complement pattern 11100110 to decimal |
| d. |
the number −23(10) to an
eight-bit two's-complement pattern |
| a. |
100101(2)
is 37(10) |
| b. |
53(10) to binary
is 110101(2) |
| c. |
the eight-bit two's-complement pattern 11100110
is −26(10) |
| d. |
−23(10)
is represented as 11101001 |
Problem Q2r.3.
Convert the below to an equivalent if statement where
not doesn't appear.
if not (a == b or a.getX() > b.getX()):
if a != b and a.getX() <= b.getX():
Problem Q2r.4.
What is displayed when the below program is executed?
def run():
a = 3
x = 4
weird(a)
weird(a)
print x
def weird(y):
x = y * 2
y = x + 5
print y
run()
Problem Q2r.5.
What is displayed when the below program is executed?
def run():
x = 3
y = weird(x)
print x, y
def weird(z):
print z
z = z * z
print z
return z + 1
run()
Problem Q2r.6.
What is displayed when the below program is executed?
def run():
a = 3
print f(a)
print a
def f(x):
print g(x)
a = x + 5
print g(a)
return a
def g(y):
y = y * 2
print y
return y + 1
run()
Problem Q2r.7.
What is displayed when the below program is executed?
def run():
a = [10]
b = [40]
addLists(a, b)
print a[0], b[0]
def addLists(x, y):
x = [x[0] + 20]
y[0] = x[0] + y[0]
run()
Problem Q2r.8.
Complete the f function below so the below program, when
executed, displays 4.0 7.0
. (Do not modify anything
except the body of f. There are many possible answers.)
def run():
a = 16.0
b = 49.0
print f(a), f(b)
def f(x):
run()
def f(x):
if x == 16.0:
return 4.0
else:
return 7.0
Two shorter alternative bodies for f:
return x ** 0.5
or
return (x + 28) / 11
Problem Q2r.9.
Complete the below function so that given a list of integers, it
returns the number of them that are even.
def count_evens(numbers):
count = 0
for num in numbers:
if num % 2 == 0:
count = count + 1
return count