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

Quiz 1: Questions

Q1.1.

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

k = 13
print 2 + k * 2
print k % 4
print 5 * (k / 5)
Q1.2.

[4 pts] What are the values referenced by a, b, and c following the execution of the below fragment?

a = 'x'
b = 'y'
c = 'z'

a = b
b = c
c = a + 'b'
Q1.3.

[6 pts] At right, tabulate the values taken on by each variable as the program fragment below executes.

k = 50
for i in range(6):
    j = 2 ** i
    if i % 2 == 0:
        k = j + k
Q1.4.

[6 pts] Assume year is an integer representing a year between 2001 and 2399. Below, complete the if statement so that it tests whether year represents a leap year. (Recall that leap years occur when the year is a multiple of 4 but not of 100. You don't need to worry about multiples of 400.)

if

    print
 year, 'is a leap year.'
Q1.5.

[8 pts] Complete the program so that it displays the characters of word in reverse order, one per line. Below is an illustration of how it should work.

Word? straw
w
a
r
t
s
word = raw_input('Word? ')

Quiz 1: Solutions

Q1.1.
28
1
10
Q1.2.
a: 'y'
b: 'z'
c: 'yb'
Q1.3.
i: 0, 1, 2, 3, 4, 5
j: 1, 2, 4, 8, 16, 32
k: 50, 51, 55, 71
Q1.4.
if year % 4 == 0 and year % 100 != 0:
    print year, 'is a leap year.'
Q1.5.
word = raw_input('Word? ')
for i in range(len(word)):
    print word[len(word) - 1 - i]

Alternative solution:

word = raw_input('Word? ')
for i in range(len(word) - 1, -1, -1):
    print word[i]