Completed
Loops
Questions From This Challenge
Q1mcq1 mark
Which loop is best suited when the number of iterations is known in advance, e.g., iterating over a range of numbers?
for loop ✓
while loop
if-else
elif chain
Q2mcq1 mark
What does `range(5)` generate when used in a for loop?
The sequence 0, 1, 2, 3, 4 ✓
The sequence 1, 2, 3, 4, 5
The sequence 0, 1, 2, 3, 4, 5
The single value 5
Q3mcq1 mark
Which loop repeats a block of statements as long as a given condition remains True, without necessarily knowing the number of iterations in advance?
while loop ✓
for loop
if statement
elif statement
Q4mcq1 mark
What is the risk if the condition in a while loop never becomes False?
The loop becomes an infinite loop ✓
The loop executes exactly once
Python automatically stops it after 10 iterations
It causes a SyntaxError
Q5mcq1 mark
What does `range(2, 10, 2)` produce?
2, 4, 6, 8 ✓
2, 4, 6, 8, 10
2, 3, 4, 5, 6, 7, 8, 9
0, 2, 4, 6, 8
Q6mcq1 mark
In a while loop, which part must eventually change to allow the loop to terminate?
The variable(s) tested in the loop's condition ✓
The name of the loop variable
The indentation level of the loop body
The order of print statements inside the loop
Q7output1 mark
What is the output of the following code?
total = 0
for i in range(1, 6):
total = total + i
print(total)
Correct output:
15Q8output1 mark
What is the output of the following code?
for i in range(0, 10, 3):
print(i)
Correct output:
0
3
6
9Q9output1 mark
What is the output of the following code?
n = 5
while n > 0:
print(n)
n = n - 1
Correct output:
5
4
3
2
1Q10output1 mark
What is the output of the following code?
i = 1
total = 0
while i <= 4:
total = total + i
i = i + 1
print(total)
Correct output:
10Q11output1 mark
What is the output of the following code?
word = "cat"
for ch in word:
print(ch)
Correct output:
c
a
tQ12output1 mark
What is the output of the following code?
for i in range(10, 0, -2):
print(i)
Correct output:
10
8
6
4
2Q13output1 mark
What is the output of the following code?
for i in range(1, 3):
for j in range(1, 3):
print(i, j)
Correct output:
1 1
1 2
2 1
2 2Q14output1 mark
What is the output of the following code?
num = 1
product = 1
while num <= 4:
product = product * num
num = num + 1
print(product)
Correct output:
24Q15output1 mark
What is the output of the following code?
for i in range(1, 5):
print(i * i)
Correct output:
1
4
9
16