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

Exam 1: Questions

X1.1.

[6 pts] What is displayed by the below sequence of Python statements?

k = 5
print 10 + 2 * k ** 2
print 127 % k
print 13/5 * k
X1.2.

[8 pts] Tabulate the values taken on by the variables i, j, k, and n as the program fragment below executes.

n = 1
for i in range(3):  
    for j in range(3):
        k = i * j
        n = n + k
X1.3.

[6 pts] The below program fragment is intended to display 6, 3, 2, 1.5, 1.2, 1, but it does not display these numbers. What numbers does it display, and how can the program be altered to work as intended?

for i in range(6):
    print 6 / (i + 1)
X1.4.

[10 pts] The harmonic number for an integer n is the sum of the reciprocals of the integers from 1 to n. For example, the harmonic number for 3 is 1 / 1  + 1 / 2  + 1 / 3 = 1 5 / 6. Complete the below so that it computes and displays the harmonic number corresponding to the integer given by the user. If the user enters 2, the program should display 1.5; for 3, it should display 1.83333333333.

n = input('Find harmonic number for: ')
X1.5.

[8 pts] Tabulate the values taken on by the variables i and result as the program fragment below executes.

word = 'bananas'
result = 'f'
for i in range(4):
    if word[i:i + 2] != 'na':
        result = result + word[i]
X1.6.

[10 pts] Complete the following program so that it displays the number of double letters in the word provided by the user. For example, if the user enters bookkeeper, your program should display 3, due to its inclusion of oo, kk, and ee; for peep, it would display just 1. (Don't worry about words having three or more consecutive copies of the same letter.)

word = raw_input('Your word? ')
X1.7.

[8 pts] What will be in the list nums after executing the following fragment?

nums = [422355]
for i in range(6):
    if nums[i] == i:
        nums[i] = nums[i - 1] + 1
X1.8.

[10 pts] We have a variable names referring to a list with several names in it. For instance, names might hold the following:

[MacArthur, Stewart, Cullen, Farquhar, MacDonald, Macaulay, Mackenzie, Campbell]

Write a program fragment displaying all strings from names that start with the letters Mac.

X1.9.

[8 pts] Sketch what appears in the window created by the below program fragment.

from graphics import *

win = GraphWin('Mystery')
for d in range(4):
    c = Point(90 - d * 10100)
    b = Circle(c, d * 10 + 10)
    b.draw(win)
X1.10.

[6 pts] Perform each of the following conversions.

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

Exam 1: Solutions

X1.1.
60
2
10
X1.2.
i:0, 1, 2
j:0,1,2,0,1,2,0,1,2
k:0,0,0,0,1,2,0,2,4
n:1,1,1,1,1,2,4,4,6,10
X1.3.

It displays 6, 3, 2, 1, 1, 1. [This is because the division has an integer on each side, so any remainder is ignored during the division.] To repair it to work with floating-point division, simply replace the print statement's 6 with 6.0:

print 6.0 / (i + 1)

An alternative solution would use the float function to convert the denominator to floating-point:

print 6 / float(i + 1)
X1.4.
sum = 0
for i in range(n):
    sum = sum + 1.0 / (i + 1)
print sum
X1.5.
i:0,1,2,3
result:f,fb,fba,fbaa
X1.6.
count = 0
for i in range(1, len(word)):
    if word[i - 1] == word[i]:
        count = count + 1
print count
X1.7.
[4, 2, 3, 4, 5, 6]
X1.8.
for name in names:
    if len(name) >= 3 and name[0:3] == 'Mac':
        print name
X1.9.
X1.10.
a. 1101(2) = 13(10)
b. 100110(2) = 38(10)
c. 22(10) = 10110(2)