Test yourself on Python For Loops - Loops allow execution of a fragment of code numerous times. Definite loops have a counter to store the number of iterations, others are called conditional loops and their execution depends on some preset conditions.

10 Question Multiple Choice Quiz with Answers and Answer Explanations

1) Which of the following pieces of code defines a list of all the numbers from 1 to 10?

2) Which of the following sequences would be generated bt the given line of code?
range (10, 0, -5)

a) 10 5
b) -5 0 5 10
c) 0 5 10
d) 10 5 0 -5

3) What is the correct term to call one execution of a loop?

a) Counter
b) Function
c) Run
d) Iteration

4) What is the value of variable sum after execution of the following code?
sum=10
for number in range (1, 10, 2): sum += number

a) 19
b) 35
c) 23
d) 45

5) There are a number of apples on the counter, lying as a pyramid. Calculate the total number of apples in 5 rows using a loop. What is the correct line of code to put inside the loop?
total=0
for row in range (1, 5):

print(total)

a) total += 1
b) total += apples
c) total += row
d) total += range

6) Single-celled amoeba divides itself into two cells every 3 hours. What is the correct piece of code to create a loop to calculate the number of amoebas in 24 hours?

8) What is the result of the execution of the following code?for letter in "word":print(letter, end="")

a) letter
b)

l
e
t
t
e
r

c)

w
o
r
d

d) word

9) What is the result of the execution of the following code?
for x in range (2):
for y in range (2):
print(x, y, sep='')

a)

00
01
10
11

b)

0 0 sep
0 1 sep
1 0 sep
1 1 sep

c)

00 01 10 11

d) The compile command.

01
10
00
11

10) Each pixel in the monitor displays colors by combination of values red, green and blue, which can take values from 0 to 255. What parameters should be put inside the range function to produce the following RGB codes (colors are presented for reference only)?
for red in range ( ): for green in range ( ): for blue in range ( ): print(red, green, blue, sep=", ")