printable version
Test 3 Review A
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
[11]
[12]
[13]
[14]
[15]
Problem R3a.1.
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('{0} {1}'.format(f(a), f(b)))
def f(x):
# your solution here
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 R3a.2.
The following program is intended to deposit $100 into an empty
account and display the resulting balance, so it should display
100.0.
Explain why it displays 0.0
instead.
def deposit(account_balance, deposit_amount):
account_balance = account_balance + deposit_amount
my_account = 0.00
deposit(my_account, 100.00)
print(my_account)
At the time of the function call, my_account
's value is copied
into the deposit
function's account_balance
variable;
but the call does not establish any link between the variables
beyond simply copying the values at this time.
Thus, when deposit
later changes the value
associated with account_balance
, this does not affect
the value associated with my_account
, and so
my_account
remains unchanged from its initial value of 0.
Problem R3a.3.
What does the below program display?
def go(x):
y = step(x)
z = step(y)
print('{0} {1} {2}'.format(x, y, z))
def step(a):
a = a + 1
return a + 1
go(5)
5 7 9
Problem R3a.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 R3a.5.
What is displayed when the below program is executed?
def run():
x = 3
y = weird(x)
print('{0} {1}'.format(x, y))
def weird(z):
print(z)
z = z * z
print(z)
return z + 1
run()
Problem R3a.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 R3a.7.
What is displayed when the below program is executed?
def run():
a = [10]
b = [40]
add_lists(a, b)
print('{0} {1}'.format(a[0], b[0]))
def add_lists(x, y):
x = [x[0] + 20]
y[0] = x[0] + y[0]
run()
Problem R3a.8.
What is displayed when the below program is executed?
def run():
a = 1
y = 2
b = process(a)
c = process(b)
print('{0} {1} {2}'.format(a, b, c))
print(y)
def process(x):
y = x + 1
x = x * 2
print(y)
return x
run()
Problem R3a.9.
How many bits do you need to represent seven different values? Nine?
Twelve? Thirty?
You need 3 bits for seven values,
4 for nine or twelve,
and 5 for thirty values.
Problem R3a.10.
How many bits are in a kilobyte of memory?
There are 8,192 bits in a kilobyte:
8 bits |
× |
1,024 bytes |
= |
8,192 bytes |
byte |
KB |
KB |
Problem R3a.11.
Perform each of the following conversions.
a. |
1101(2) |
to decimal |
b. |
100110(2) |
to decimal |
c. |
22(10) |
to binary |
a. |
1101(2) = |
13(10) |
b. |
100110(2) = |
38(10) |
c. |
22(10) = |
10110(2) |
Problem R3a.12.
Perform each of the following conversions.
a. |
100101(2) to decimal |
b. |
53(10) to binary |
a. |
100101(2)
is 37(10) |
b. |
53(10) to binary
is 110101(2) |
Problem R3a.13.
Perform each of the following conversions.
a. | 101101(2) | to decimal |
b. | 1010101(2) | to decimal |
c. | 23(10) | to binary |
d. | 95(10) | to binary |
a. | 101101(2) | is 45(10) |
b. | 1010101(2) | is 85(10) |
c. | 23(10) | is 10111(2) |
d. | 95(10) | is 1011111(2) |
Problem R3a.14.
Explain what ASCII is and what ASCII's purpose is.
ASCII is a mapping between distinct seven-bit values and
the characters found on an American keyboard (plus some
additional non-printable symbols). This is used for encoding
text in files and in network communications.
Problem R3a.15.
Identify three categories of characters that are defined in Unicode
but not in ASCII.
There are many answers here, but some possibilities:
accented characters for other European languages (like
ñ, é, and ö),
Chinese characters (Han alphabet),
the Cherokee alphabet, the Greek alphabet,
the Arabic alphabet, the Hebrew alphabet, Egyptian hieroglyphics,
playing cards, emoticons, mathematical symbols,
and musical notes and symbols.