Practice Problems - Set 0
These problems are for practice. They are optional, but you are encouraged to complete them and discuss them with members of the course staff during lab and office hours.
Some of these problems will appear on the final exam, and others are very similar to final exam problems. However, not all final exam problems will be telegraphed.
Problems 0 - 2 reference the function definition below.
def converter(x):
if x < 0:
return(-1 * x)
elif x > 1:
return(x)
elif x == 0:
return(1)
else:
return(0)
Problem 0
What is printed by the following program?
for j in range(5):
= converter(j)
k print(k, end=" ")
Problem 1
What is printed by the following program?
for j in range(7):
= j % 3
k = converter(k)
m print(m, end=" ")
Problem 2
Modify Line 4 of the converter
function such that the printed output of:
for j in range(10):
= converter(j)
k print(k, end=" ")
is
1 0 2 0 4 0 6 0 8 0
Problem 3
This program intends to print:
1
22
333
It does not produce this output. Rewrite Line 2 so that this output is produced.
Problem 4
Several of these programs produce the same output. Identify them.
A.
B.
C.
D.
E.
Problem 5
What is printed by the following program?
Problem 6
What is printed by the following program?
Problem 7
Given this program:
Modify Line 1 such that the output is:
1 2 3 4 5
Problem 8
Rewrite the function triangle(size)
to use two while
loops rather than two for
loops.
Problem 9
Rewrite the function triangle(size)
to use one for
loop rather than two for
loops.