You are on page 1of 4

1. What type is the following variable?

x = “Hi there”

a) float b) integer c) boolean d) string

2.How many lines will this program print? while True:

print “hi” infinity num of lines

3.How many lines will this program print?

x = 10 while x > 0:

print x

x=x-3

4 lines

4.Which of the following programs prints ten lines?

a) for i in range(10):

print “hi”
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
9. How many lines does the following program print?

for i in range(3): for j in range(5):

print “hi”

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

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

13. Which of the following for loops would print the following numbers?

012345

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

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
Write the code that results in the following output: [‘apples’, ‘bananas’, ‘grapes’, ‘oranges’,
‘pineapples’]

17. What does this program print?

my_list = [-4, 2, 3, 2, -2, 5]


print [x % 2 == 0 for x in my_list]

18. Which of the following lines of code will cause an error? Use the following definition of ages:
ages = (12, 5, 8)
a) ages = ages + (1, 3, 5)

b) print ages[2]
c) ages = ages[2:] d) ages[0] = 3
20. What is the value of num after this code runs?

shapes = [“triangle”, “square”, “hexagon”, “circle”, “pentagon”] num = len(shapes)

You might also like