b) for i = 1 to 10:
print “hi”
c) for i in 1 - 10:
print “hi”
d) for i from 0 to 9:
print “hi”
5. Which of the following best describes the purpose of a for loop?
a) A for loop is for doing something an indeterminate number of times.
b) A for loop is doing something an infinite number of times.
c) A for loop is for doing something a fixed number of times.
d) A for loop is for doing something three times.
6. Which Python keyword skips back to the beginning of a loop?
a) break
b) continue
7. Which Python keyword exits a loop?
a) break
b) continue
8. What does the following program print?
for i in range(2):
for j in range(2):
print i + j
a) 0
1
1
2
b) 0112
c) 0
1
0
1
d) 0101
9. How many lines does the following program print?
for i in range(3):
for j in range(5):
print “hi”
a) 3
b) 5
c) 8
d) 15
10. Which of the following while loops would continue to loop as long as num is in the range 3 to 12, exclusive?
a) while num > 12 and num < 3:
# do something with num
b) while num < 12 or num > 3:
# do something with num
c) while num <= 12 and num >= 3:
# do something with num
d) while num < 12 and num > 3:
# do something with num
11. What is the value of sum when this loop completes?
sum = 0
for i in range(3):
sum = sum + 5
for j in range(2):
sum = sum - 1
a) 8
b) 9
c) 20
d) 0
12. Which of the following for loops would print the following numbers?
3
5
7
9
a) for i in range(3, 10, 2):
print i
b) for i in range(3, 9, 2):
print i
c) for i in range(9):
print i
d) for i in range(3,9):
print i
13. Which of the following for loops would print the following numbers?
0
1
2
3
4
5
a) for i in range(5):
print i
b) for i in range(1, 5, 1):
print i
c) for i in range(6):
print i
d) for i in range(0,5, 1):
print i
14. What does this program print?
for i in range(6):
if i == 3:
continue
print i
a) 0
1
2
b) 0
1
2
3
c) 0
2
4
6
d) 0
1
2
4
5
15. Which of the following Python programs creates a list with the numbers 1 through 5?
a) my_list = (1, 2, 3, 4, 5)
b) my_list = [1, 2, 3, 4, 5]
c) my_list = 1, 2, 3, 4, 5
d) my_list = “1, 2, 3, 4, 5”
16. Look at the following program:
my_list = [“bananas”, “oranges”, “grapes”,
“pineapples”, “apples”]
# You pick the code that goes here...
# ...
# ...
print my_list
Pick the code that results in the following output:
[‘apples’, ‘bananas’, ‘grapes’, ‘oranges’, ‘pineapples’]
a) my_list.sort()
my_list.reverse()
b) my_list.sort()
c) my_list.reverse()
d) my_list.remove(“grapes”)
17. What does this program print?
my_list = [-4, 2, 3, 2, -2, 5]
print [x % 2 == 0 for x in my_list]