NEW
Font size
WorksheetsProgramming Loops Quiz
Total questions: 25
Worksheet time: 13mins
What is the main purpose of loops in programming?
To make code longer
To automate repetitive tasks
To create errors
To slow down programs
What will this code print?
1, 2, 3, 4, 5
0, 1, 2, 3, 4
0, 1, 2, 3, 4, 5
1, 2, 3, 4
What is the key difference between for loops and while loops?
For loops are faster
For loops repeat N times (known iterations), while loops repeat until condition is False (unknown iterations)
While loops are better
There is no difference
What does range(1, 6) generate?
1, 2, 3, 4, 5, 6
0, 1, 2, 3, 4, 5
1, 2, 3, 4, 5
2, 3, 4, 5, 6
What will range(0, 11, 2) generate?
0, 2, 4, 6, 8, 10
0, 2, 4, 6, 8, 10, 12
2, 4, 6, 8, 10
1, 3, 5, 7, 9, 11
What does the third parameter in range(start, stop, step) represent?
The starting number
The stopping number
The increment/step value
The number of iterations
What will this code print?
5, 4, 3, 2, 1
0, 1, 2, 3, 4, 5
5, 4, 3, 2, 1, 0
1, 2, 3, 4, 5
What is the most critical thing to remember when writing while loops?
Use proper indentation
Always update the condition variable to avoid infinite loops
Use print statements
Start from 0
What will this code print?
1, 2, 3
1, 2, 3, 4
0, 1, 2, 3
Infinite loop
What happens if you forget to update the condition variable in a while loop?
The program runs faster
The loop creates an infinite loop
The loop runs once
Nothing happens
What does the 'break' statement do?
Skips the current iteration
Exits the loop completely
Pauses the loop
Restarts the loop
What does the 'continue' statement do?
Exits the loop completely
Skips the current iteration and moves to the next one
Continues to the next line
Does nothing
What will this code print?
1, 2, 3, 4, 5
1, 2
1, 2, 3
4, 5
What will this code print?
1, 2, 3, 4, 5
1, 2, 4, 5
3
1, 2
What is the output of this code?
3
6
10
4
In Python, does range() include the stop value?
Yes, always
No, it stops BEFORE the stop value
Sometimes
Only with negative step
What will this code print?
All numbers 1-10
Only odd numbers: 1, 3, 5, 7, 9
Only even numbers: 2, 4, 6, 8, 10
Nothing
Which loop structure is best when you know exactly how many times to repeat?
while loop
for loop
Both are equally good
Neither
Which loop structure is best for input validation (keep asking until valid)?
for loop
while loop
if statement
elif statement
What will this code do?
Run forever with no way to stop
Run until user types "quit"
Run once
Cause an error
What is the result of range(5)?
1, 2, 3, 4, 5
0, 1, 2, 3, 4
0, 1, 2, 3, 4, 5
5
How do you create a countdown from 10 to 1 using range()?
range(10, 1)
range(10, 0, -1)
range(1, 10)
range(10, 1, -1)
What is wrong with this code?
Missing increment (count should be updated)
Wrong condition
Missing colon
Nothing is wrong
What will this code print?
Python (once)
Python (twice)
Python (three times)
0, 1, 2
In a password retry system with 3 attempts, which loop is most appropriate?
for loop with range(3)
while loop with attempt counter
Either could work
No loop needed
