wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Mastering Python Loops

Total questions: 50

Worksheet time: 41mins

Name
Class
Date
1.

What is the syntax for a for loop in Python?

a)

for index in range(n):

b)

for each item in list:

c)

for element of collection:

d)

for variable in sequence:

2.

Explain how a while loop works in Python.

a)

A while loop in Python runs a block of code once if the condition is true.

b)

A while loop in Python iterates through a list of items based on their index.

c)

A while loop in Python executes a block of code until a condition becomes false.

d)

A while loop in Python executes a block of code repeatedly as long as a specified condition is true.

3.

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

a)

The program will enter an infinite loop.

b)

The program will skip the loop entirely.

c)

The loop will run a fixed number of times.

d)

The program will terminate immediately.

4.

Which of the following is mandatory in a while loop?

a)

continue

b)

break

c)

updation

d)

condition

5.

How can you exit a while loop prematurely?

a)

Use the 'continue' statement.

b)

Use the 'break' statement.

c)

Use the 'return' statement.

d)

Use the 'exit' command.

6.

What is the key feature of a while loop?

a)

Runs once

b)

Runs for fixed iterations

c)

Runs as long as a condition is true

d)

Runs only with numbers

7.

What are the default values used by range(n)?

a)

start = 1, step = 1

b)

start = 0, step = 1

c)

start = n, step = 1

d)

start = 0, step = 0

8.

Why does range(5, 0, -1) work correctly?

a)

Uses < condition

b)

Uses > condition

c)

Uses <= condition

d)

Uses >= condition

9.

When are the values of a range() generated?

a)

At program start

b)

When range() is defined

c)

When loop requests the next value

d)

When converted to list

10.

What are loop control statements in Python?

a)

Loop control statements in Python are 'for', 'while', and 'if'.

b)

Loop control statements in Python consist of 'start', 'end', and 'repeat'.

c)

Loop control statements in Python include 'loop', 'exit', and 'return'.

d)

Loop control statements in Python include 'break' and 'continue'.

11.

Explain the use of break in a loop.

a)

The 'break' statement continues the loop without any changes.

b)

The 'break' statement pauses the loop until a condition is met.

c)

The 'break' statement skips the current iteration of the loop.

d)

The 'break' statement terminates the loop and transfers control to the statement following the loop.

12.

Which loop is preferred when the number of iterations is fixed?

a)

while

b)

do while

c)

for

d)

infinite

13.

What does the continue statement do in a loop?

a)

The continue statement pauses the loop for a specified duration.

b)

The continue statement restarts the loop from the beginning.

c)

The continue statement skips the current iteration of a loop and proceeds to the next iteration.

d)

The continue statement ends the loop and exits immediately.

14.

How can you create a pattern using nested loops?

a)

Use a single loop to iterate through all elements in a list.

b)

Create a pattern by using only conditional statements without loops.

c)

Use nested loops where the outer loop iterates over rows and the inner loop iterates over columns to print the desired pattern.

d)

Utilize recursion to generate the pattern without any loops.

15.

Write a Python program to print a triangle pattern using loops.

a)

for i in range(1, 6): print('*' * i)

b)

for i in range(1, 7): print('+' * i)

c)

for i in range(1, 5): print('o' * (i + 1))

d)

for i in range(1, 4): print('#' * i)

16.

What is the output?

a)

0 1 2

b)

End

c)

0 1 2 End

d)

0 1 2 End

17.

What is the difference between while-else and for-else?

a)

'while-else' is used for error handling, while 'for-else' is for data processing.

b)

'while-else' executes only if the loop is interrupted, while 'for-else' runs unconditionally.

c)

Both 'while-else' and 'for-else' are interchangeable in their usage and functionality.

d)

The difference is that 'while-else' is used with while loops and 'for-else' is used with for loops, both executing 'else' when the loop completes without a break.

18.

Provide an example of a series generated using a for loop.

a)

987, 24, 6

b)

2, 4, 6, 8, 10

c)

15, 45, 136, 68, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1

d)

72, 36, 18, 9, 27, 81, 243

19.

Which of the following series is most appropriately generated using a while loop, since the next term depends on the previous value and the number of iterations is not known in advance?

a)

1, 4, 9, 16, 25

b)

2, 4, 6, 8, 10

c)

0, 1, 2, 3, 4

d)

100, 50, 25, 12, 6, 3, 1

20.

What is the output?

a)

Completed

b)

No output

c)

2

d)

Error

21.

How can you use a while loop to generate a Fibonacci series?

a)

Initialize a, b = 1, 2;

while count < n:

print(b);

a, b = b, a + b;

b)

Initialize a, b = 0, 1;

while count < n:

print(a);

a, b = b, a + b;

c)

Start with a, b = 0, 1;

for i in range(n):

print(a);

a, b = b, a + b;

d)

Set a, b = 1, 1;

while n > 0:

print(b);

b, a = a + b, b;

22.

What is the output of the following code:

for i in range(5):

print(i) ?

a)

5

b)

-1

c)

3.5

d)

0 1 2 3 4

23.

How do you handle infinite loops in Python?

a)

Replace the loop with a recursive function.

b)

Use print statements to debug the loop.

c)

Ignore the loop and let it run indefinitely.

d)

Use break conditions, timeouts, or exception handling to manage infinite loops.

24.

Find Answer

a)

55

b)

60

c)

50

d)

45

25.

What will be the output of the following code: for i in range(3): print(i * 2)?

a)

0 1 2

b)

0 2 4

c)

0 2 4 6

d)

1 2 3

26.

How can you create a countdown using a while loop in Python?

a)

while count > 0: print(count) count -= 1

b)

while count >= 0: print(count) count = count + 1

c)

while count < 0: print(count) count += 1

d)

while count != 0: print(count) count = count - 1

27.

What will be the result of the following code snippet? for i in range(5): if i == 3: break print(i)

a)

0 1 2 3

b)

1 2 3 4

c)

0 1 2

d)

0 1 2 3 4

28.

Find Answer

a)

25

b)

30

c)

55

d)

20

29.

Find answer

a)

Prints 55

b)

Infinite loop

c)

Indentation Error

d)

No output

30.

Find answer

a)

Prints 10 to 0

b)

Prints 10 once

c)

Infinite loop

d)

Error

31.

Find answer

a)

Infinite loop

b)

Value Error

c)

Prints nothing

d)

Syntax error

32.

Find answer

a)

Infinite loop

b)

Runtime error

c)

5

d)

No output (logical error)

33.

Find answer

a)

Prints Hello once

b)

Infinite loop

c)

No output

d)

Error

34.

What will be the output?

a)

0 1 2 3 4

b)

2 4 6 8 10

c)

2 3 4 5 6

d)

5 6

35.

What happens?

a)

Infinite loop

b)

Prints 0 to 4 twice

c)

Prints 0 to 4 once

d)

Error

36.

Output?

a)

5 4 3 2 1

b)

0

c)

No output

d)

Error

37.

Output?

a)

0 1 2

b)

Infinite loop

c)

Error

d)

0 1

38.

What happens?

a)

Infinite loop

b)

Prints "Loop" once

c)

Error

d)

No output

39.

What happens?

a)

0 1 2

b)

0 only

c)

Infinite loop

d)

Error

40.

Output?

a)

5 4 3 2 1 0

b)

5 4 3 2 1

c)

Infinite loop

d)

Error

41.

Output?

a)

012

012

012

b)

000

111

222

c)

000

000

000

d)

012

123

234

42.

Output?

a)

*

**

***

b)

***

**

*

c)

****

***

**

d)

***

***

***

43.

Output?

a)

0

01

012

b)

0

01

c)

(empty line)

0

01

d)

0

1

2

44.

Output?

a)

*

**

***

b)

***

**

*

c)

*

*

*

d)

******

45.

Output?

a)

*

*

*

b)

*

*

*

*

*

*

c)

**

**

**

d)

***

46.

Output?

a)

*

**

***

b)

***

**

*

c)

****

***

**

d)

***

***

***

47.

Output?

a)

1 3 5

b)

3 5 7

c)

2 4 6

d)

Infinite loop

48.

Output?

a)

1 2 3 4 5

b)

1 2 X 4 5

c)

X X

d)

Error

49.

Output?

a)

0 1 2 3 4

b)

4

c)

No output

d)

Infinite loop

50.

Output?

a)

0 1 2 3 4

b)

0 1 2 3

c)

4

d)

No output