CSci 150: Foundations of computer science I
Home Syllabus Assignments Tests

Quiz 2 Review: Questions

Q2r.1.
Show your intermediate work on each of the following.
  1. What base-10 number does the eight-bit two's-complement bit pattern 01010101 represent?
  2. What base-10 number does the eight-bit two's-complement bit pattern 10101010 represent?
  3. Convert 26(10) to an eight-bit two's-complement representation.
  4. Convert −104(10) to an eight-bit two's-complement representation.
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
Q2r.3.

Convert the below to an equivalent if statement where not doesn't appear.

if not (a == b or a.getX() > b.getX()):
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()
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()
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()
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()
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()
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):

Quiz 2 Review: Solutions

Q2r.1.
a.85(10)
b.−86(10)
c.00011010
d.10011000
Q2r.2.
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
Q2r.3.
if a != b and a.getX() <= b.getX():
Q2r.4.
11
11
4
Q2r.5.
3
9
3 10
Q2r.6.
6
7
16
17
8
3
Q2r.7.
10 70
Q2r.8.
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

Q2r.9.
    count = 0
    for num in numbers:
        if num % 2 == 0:
            count = count + 1
    return count