wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Understanding Loops in Programming

Total questions: 22

Worksheet time: 12mins

Name
Class
Date
1.

What is the correct syntax for a basic for loop in Python?

a)

`for i in range(10):`

b)

`for (i = 0; i < 10; i++)`

c)

`for i to 10`

d)

`for i = 1 to 10`

2.

Which of the following is the correct syntax for a while loop in Python?

a)

`while (i < 10)`

b)

`while i < 10:`

c)

`while i = 10`

d)

`while (i < 10):`

3.

In a for loop, what does the `range()` function do?

a)

It creates a list of numbers.

b)

It generates a sequence of numbers.

c)

It sorts a list of numbers.

d)

It reverses a list of numbers.

4.

What is the purpose of a loop in programming?

a)

To execute a block of code once.

b)

To execute a block of code multiple times.

c)

To stop the execution of a program.

d)

To create a new variable.

5.

Which of the following is a common error when using loops?

a)

Infinite loop

b)

Syntax error

c)

Variable declaration

d)

Function definition

6.

Which of the following is an example of a simple for loop in Python?

a)

`for i in range(5): print(i)`

b)

`for i < 5: print(i)`

c)

`for i = 0 to 5: print(i)`

d)

`for i in 5: print(i)`

7.

What will the following code output? ```python for i in range(3): print(i) ```

a)

0 1 2

b)

1 2 3

c)

0 1 2 3

d)

1 2

8.

What is the result of the following while loop? ```python i = 0 while i < 3: print(i) i += 1 ```

a)

0 1 2

b)

1 2 3

c)

0 1 2 3

d)

1 2

9.

Which of the following is NOT a common loop error?

a)

Infinite loop

b)

Off-by-one error

c)

Syntax error

d)

Variable mismatch

10.

What is the purpose of the `range()` function in a for loop?

a)

To specify the number of times the loop should run.

b)

To create a new variable.

c)

To end the loop.

d)

To reverse the loop.

11.

Which of the following is a correct example of a nested loop?

a)

`for i in range(3): for j in range(3): print(i, j)`

b)

`for i in range(3): print(i) for j in range(3): print(j)`

c)

`for i in range(3): print(i) while j < 3: print(j)`

d)

`while i < 3: for j in range(3): print(i, j)`

12.

What is the output of the following code? ```python i = 0 while i < 2: print(i) i += 1 ```

a)

0 1

b)

1 2

c)

0 1 2

d)

1 2 3

13.

Which of the following is a common mistake when using a while loop?

a)

Forgetting to increment the loop variable

b)

Using the wrong variable type

c)

Using a for loop instead

d)

Declaring a variable inside the loop

14.

What is the output of the following code? ```python for i in range(1, 4): print(i) ```

a)

1 2 3

b)

0 1 2

c)

1 2 3 4

d)

2 3 4

15.

What is the output of the following code? ```python i = 1 while i <= 3: print(i) i += 1 ```

a)

1 2 3

b)

0 1 2

c)

1 2 3 4

d)

2 3 4

16.

Which of the following is an example of an infinite loop?

a)

`while True: print("Hello")`

b)

`for i in range(10): print(i)`

c)

`while i < 10: print(i)`

d)

`for i in range(5): print(i)`

17.

What is the output of the following code? ```python for i in range(2): for j in range(2): print(i + j) ```

a)

0 1 1 2

b)

0 1 2 3

c)

1 2 3 4

d)

0 1 0 1

18.

Loops in programming start counting from ?

a)

0

b)

1

c)

Any number

19.

Statement lets you run code many times while a condition is true

a)

When loop

b)

Condition loop

c)

while loop

d)

Parentheses loop

20.

Which lines of code will be repeated over and over forever?

a)

All Lines

b)

Lines 3-8

c)

Lines 5-8

d)

Lines 5-6

e)

None

21.

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.

a)

FOR Loop

b)

WHILE Loop

22.

What is a loop within another loop called?

a)

Nested loop

b)

While Loop

c)

Nested Conditional

d)

For Loop