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

Quiz 1 Review: Questions

Q1r.1.

What is displayed by the below sequence of Python statements?

k = 31
print 64 - k * 2
print k % 8
print 8 * (k / 8)
Q1r.2.

What is displayed by the below sequence of Python statements?

k = 18
x = 3.5
print k + x
print 22/5 * x
print k % 7
print 1 + x * 2
Q1r.3.

List the values of a, b, and c following the execution of the below fragment.

a = 10
b = 20
c = 40

a = b
a = c
Q1r.4.

List the values of a, b, and c following the execution of the below fragment.

a = 10
b = 20
c = 40

a = b
b = c
c = a
Q1r.5.

Suppose we wish to enter the below if statement's body if the square of x isn't between 99 and 101. What condition belongs in the parentheses?

if                                 :
    x = (x + 1.0 / x) / 2.0
Q1r.6.

Suppose we have a vertical line segment from (lx,ly0) to (lx,ly1) (where ly0 < ly1). What should go into an if statement's condition to test whether a point (px,py) lies on that line?

if                            :

  print 'point on line'
Q1r.7.

Tabulate the values taken on by each variable as the program fragment below executes.

n = 32
k = n + 1
for i in range(27):
    if n % i == 0:
        k = k + i + (n / i)
Q1r.8.

The below program reads 100 numbers from the user. Modify the program so that after the loop is complete, the program displays the sum of all numbers the user typed.

for index in range(100):
    number = input('Number? ')
Q1r.9.

What are the first and last lines displayed by the below program fragment?

for i in range(12344321):
    s = str(i)
    if s[1] == '5':
        print s
Q1r.10.

Tabulate the values taken on by the variables i, j, k, and res as the program fragment below executes.

base = 'barber'
res = 'a'
for i in range(len(base)):
    k = -1
    for j in range(i):
        if base[j] == base[i]:
            k = j
    if k < 0:
        res = res + base[i]
print res
Q1r.11.

Complete the following program so that it displays the number of lower-case r's in the string entered by the user.

line = raw_input('Line of text: ')
Q1r.12.

Complete the program below so that it computes another string containing every other letter from the string typed by the user. For example, if the user enters blackberry, the program should compute baker, and if the user enters steadfastness, the program should compute sedates.

Note that your program should build up its result in the out variable rather than display letters as it goes through the string.

in = raw_input('Give me a line: ')
out = ''

# your solution here

print 'Result is:', out

Quiz 1 Review: Solutions

Q1r.1.
2         displayed for 64 - k * 2
7         displayed for k % 8
24        displayed for 8 * (k / 8)
Q1r.2.
21.5      displayed for k + x
14.0      displayed for 22/5 * x
4         displayed for k % 7
8.0       displayed for 1 + x * 2
Q1r.3.
a:40
b:20
c:40
Q1r.4.
a:20
b:40
c:20
Q1r.5.
x * x < 99 or x * x > 101
Q1r.6.
px == lx and py >= ly0 and py <= ly1
Q1r.7.
n 32
k 33, 51, 63
i 2, 3, 4, 5, 6
Q1r.8.
total = 0
for index in range(100):
    number = input('Number? ')
    total = total + number
print total
Q1r.9.

The first is 1500 and the last is 3599.

Q1r.10.
i 0, 1, 2, 3, 4, 5
j 0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4
k −1, −1, −1, −1, 0, −1, −1, 2
res ab, aba, abar, abare

The program displays abare.

Q1r.11.
count = 0
for i in range(len(line)):
    if line[i] == 'r':
        count = count + 1
print count
Q1r.12.
for i in range(0, len(in), 2):
    out = out + in[i]

An alternative solution:

for i in range(len(in)):
    if i % 2 == 0:
        out = out + in[i]