wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Understanding Python While Loops

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

What is the basic syntax of a while loop in Python?

a)

repeat condition: # code to execute

b)

for condition: # code to execute

c)

do while condition: # code to execute

d)

while condition: # code to execute

2.

How do you start a while loop in Python?

a)

while condition:

b)

do while condition:

c)

repeat until condition:

d)

for condition in iterable:

3.

What keyword is used to end a while loop prematurely?

a)

exit

b)

return

c)

continue

d)

break

4.

What happens if the condition in a while loop is always true?

a)

The program enters an infinite loop.

b)

The program skips the loop entirely.

c)

The program terminates immediately.

d)

The loop runs a fixed number of times.

5.

Can you use a while loop without an initial variable?

a)

You need a counter variable

b)

No, it's not possible

c)

Only with a defined condition

d)

Yes

6.

What is the difference between a while loop and a for loop?

a)

A while loop iterates a fixed number of times, while a for loop continues until a condition is met.

b)

A while loop is used for iterating over a range, while a for loop checks the condition before each iteration.

c)

A while loop checks the condition before each iteration, while a for loop is used for iterating over a range or collection with a defined structure.

d)

A while loop is more efficient than a for loop, which is slower in execution.

7.

How do you ensure that a while loop will eventually stop?

a)

Ensure the loop condition will eventually become false by modifying a variable within the loop.

b)

Avoid modifying any variables inside the loop.

c)

Use a break statement without any conditions.

d)

Keep the loop condition constant throughout the execution.

8.

What is a common use case for a while loop in programming?

a)

To iterate over a list of items.

b)

To execute a block of code as long as a condition is true.

c)

To define a function that returns a value.

d)

To create a static array of values.

9.

What will happen if you forget to update the variable in a while loop?

a)

The loop will run indefinitely.

b)

The loop will execute only once.

c)

The loop will execute a fixed number of times.

d)

The program will throw an error.

10.

What is the output of the following code?

x = 1

while x < 4:

print(x)

x += 1

a)

1

b)

123

c)

1234

d)

234

11.

What does this code print?

i = 5

while i > 2:

print("Hi")

i -= 1

a)

Hi

b)

HiHi

c)

HiHiHi

d)

nothing

12.

What is the output?

num = 0

while num <= 3:

print(num)

num = num + 2

a)

0 1 2 3

b)

0 2 4

c)

0 2

d)

1 3

13.

What does the loop print?

count = 3

while count != 0:

print(count)

count = count - 1

a)

3 2 1 0

b)

2 1 0

c)

3 2 1

d)

1 2 3

14.

Find the output:

x = 1

while x < 10:

x = x * 2

print(x)

a)

2

b)

4

c)

8

d)

16

15.

what will be the output :

i = 10

while i > 8:

print(i)

i -= 1

a)

10 9 8

b)

10 9

c)

9 8

d)

9 8 7