CSci 150: Foundations of computer science
Home Syllabus Readings Projects Tests

Test 3 Review A: Questions

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()
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_balancedeposit_amount):
    account_balance = account_balance + deposit_amount

my_account = 0.00
deposit(my_account100.00)
print(my_account)
R3a.3.

What does the below program display?

def go(x):
    y = step(x)
    z = step(y)
    print('{0} {1} {2}'.format(xyz))

def step(a):
    a = a + 1
    return a + 1

go(5)
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()
R3a.5.

What is displayed when the below program is executed?

def run():
    x = 3
    y = weird(x)
    print('{0} {1}'.format(xy))

def weird(z):
    print(z)
    z = z * z
    print(z)
    return z + 1

run()
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()
R3a.7.

What is displayed when the below program is executed?

def run():
    a = [10]
    b = [40]
    add_lists(ab)
    print('{0} {1}'.format(a[0], b[0]))

def add_lists(xy):
    x = [x[0] + 20]
    y[0] = x[0] + y[0]

run()
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(abc))
    print(y)

def process(x):
    y = x + 1
    x = x * 2
    print(y)
    return x

run()
R3a.9.
How many bits do you need to represent seven different values? Nine? Twelve? Thirty?
R3a.10.
How many bits are in a kilobyte of memory?
R3a.11.

Perform each of the following conversions.

a. 1101(2) to decimal
b. 100110(2) to decimal
c. 22(10) to binary
R3a.12.

Perform each of the following conversions.

a. 100101(2) to decimal
b. 53(10) to binary
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
R3a.14.

Explain what ASCII is and what ASCII's purpose is.

R3a.15.

Identify three categories of characters that are defined in Unicode but not in ASCII.

Test 3 Review A: Solutions

R3a.1.
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

R3a.2.

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.

R3a.3.
5 7 9
R3a.4.
11
11
4
R3a.5.
3
9
3 10
R3a.6.
6
7
16
17
8
3
R3a.7.
10 70
R3a.8.
2
3
1 2 4
2
R3a.9.
You need 3 bits for seven values, 4 for nine or twelve, and 5 for thirty values.
R3a.10.
There are 8,192 bits in a kilobyte:

8 bits × 1,024 bytes = 8,192 bytes
byte KB KB
R3a.11.
a. 1101(2) = 13(10)
b. 100110(2) = 38(10)
c. 22(10) = 10110(2)
R3a.12.
a. 100101(2) is 37(10)
b. 53(10) to binary is 110101(2)
R3a.13.
a.101101(2)is 45(10)
b.1010101(2)is 85(10)
c.23(10)is 10111(2)
d.95(10)is 1011111(2)
R3a.14.

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.

R3a.15.

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.