printable version
Quiz 1
[1]
[2]
[3]
[4]
[5]
Problem 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)
Problem 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'
Problem 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
i: |
|
0, |
1, |
2, |
3, |
4, |
5
|
j: |
|
1, |
2, |
4, |
8, |
16, |
32
|
k: |
50, |
51, |
|
55, |
|
71 |
Problem 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.'
if year % 4 == 0 and year % 100 != 0:
print year, 'is a leap year.'
Problem 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? ')
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]