WorksheetsUnderstanding Loops in Programming
Total questions: 22
Worksheet time: 12mins
What is the correct syntax for a basic for loop in Python?
`for i in range(10):`
`for (i = 0; i < 10; i++)`
`for i to 10`
`for i = 1 to 10`
Which of the following is the correct syntax for a while loop in Python?
`while (i < 10)`
`while i < 10:`
`while i = 10`
`while (i < 10):`
In a for loop, what does the `range()` function do?
It creates a list of numbers.
It generates a sequence of numbers.
It sorts a list of numbers.
It reverses a list of numbers.
What is the purpose of a loop in programming?
To execute a block of code once.
To execute a block of code multiple times.
To stop the execution of a program.
To create a new variable.
Which of the following is a common error when using loops?
Infinite loop
Syntax error
Variable declaration
Function definition
Which of the following is an example of a simple for loop in Python?
`for i in range(5): print(i)`
`for i < 5: print(i)`
`for i = 0 to 5: print(i)`
`for i in 5: print(i)`
What will the following code output? ```python for i in range(3): print(i) ```
0 1 2
1 2 3
0 1 2 3
1 2
What is the result of the following while loop? ```python i = 0 while i < 3: print(i) i += 1 ```
0 1 2
1 2 3
0 1 2 3
1 2
Which of the following is NOT a common loop error?
Infinite loop
Off-by-one error
Syntax error
Variable mismatch
What is the purpose of the `range()` function in a for loop?
To specify the number of times the loop should run.
To create a new variable.
To end the loop.
To reverse the loop.
Which of the following is a correct example of a nested loop?
`for i in range(3): for j in range(3): print(i, j)`
`for i in range(3): print(i) for j in range(3): print(j)`
`for i in range(3): print(i) while j < 3: print(j)`
`while i < 3: for j in range(3): print(i, j)`
What is the output of the following code? ```python i = 0 while i < 2: print(i) i += 1 ```
0 1
1 2
0 1 2
1 2 3
Which of the following is a common mistake when using a while loop?
Forgetting to increment the loop variable
Using the wrong variable type
Using a for loop instead
Declaring a variable inside the loop
What is the output of the following code? ```python for i in range(1, 4): print(i) ```
1 2 3
0 1 2
1 2 3 4
2 3 4
What is the output of the following code? ```python i = 1 while i <= 3: print(i) i += 1 ```
1 2 3
0 1 2
1 2 3 4
2 3 4
Which of the following is an example of an infinite loop?
`while True: print("Hello")`
`for i in range(10): print(i)`
`while i < 10: print(i)`
`for i in range(5): print(i)`
What is the output of the following code? ```python for i in range(2): for j in range(2): print(i + j) ```
0 1 1 2
0 1 2 3
1 2 3 4
0 1 0 1
Loops in programming start counting from ?
0
1
Any number
Statement lets you run code many times while a condition is true
When loop
Condition loop
while loop
Parentheses loop
Which lines of code will be repeated over and over forever?
All Lines
Lines 3-8
Lines 5-8
Lines 5-6
None
Which kind of loop would be used?
I need a guessing game program that will let me keep guessing until I get the right answer.
FOR Loop
WHILE Loop
What is a loop within another loop called?
Nested loop
While Loop
Nested Conditional
For Loop
