WorksheetsMastering Python Loops
Total questions: 50
Worksheet time: 41mins
What is the syntax for a for loop in Python?
for index in range(n):
for each item in list:
for element of collection:
for variable in sequence:
Explain how a while loop works in Python.
A while loop in Python runs a block of code once if the condition is true.
A while loop in Python iterates through a list of items based on their index.
A while loop in Python executes a block of code until a condition becomes false.
A while loop in Python executes a block of code repeatedly as long as a specified condition is true.
What will happen if the condition in a while loop is always true?
The program will enter an infinite loop.
The program will skip the loop entirely.
The loop will run a fixed number of times.
The program will terminate immediately.
Which of the following is mandatory in a while loop?
continue
break
updation
condition
How can you exit a while loop prematurely?
Use the 'continue' statement.
Use the 'break' statement.
Use the 'return' statement.
Use the 'exit' command.
What is the key feature of a while loop?
Runs once
Runs for fixed iterations
Runs as long as a condition is true
Runs only with numbers
What are the default values used by range(n)?
start = 1, step = 1
start = 0, step = 1
start = n, step = 1
start = 0, step = 0
Why does range(5, 0, -1) work correctly?
Uses < condition
Uses > condition
Uses <= condition
Uses >= condition
When are the values of a range() generated?
At program start
When range() is defined
When loop requests the next value
When converted to list
What are loop control statements in Python?
Loop control statements in Python are 'for', 'while', and 'if'.
Loop control statements in Python consist of 'start', 'end', and 'repeat'.
Loop control statements in Python include 'loop', 'exit', and 'return'.
Loop control statements in Python include 'break' and 'continue'.
Explain the use of break in a loop.
The 'break' statement continues the loop without any changes.
The 'break' statement pauses the loop until a condition is met.
The 'break' statement skips the current iteration of the loop.
The 'break' statement terminates the loop and transfers control to the statement following the loop.
Which loop is preferred when the number of iterations is fixed?
while
do while
for
infinite
What does the continue statement do in a loop?
The continue statement pauses the loop for a specified duration.
The continue statement restarts the loop from the beginning.
The continue statement skips the current iteration of a loop and proceeds to the next iteration.
The continue statement ends the loop and exits immediately.
How can you create a pattern using nested loops?
Use a single loop to iterate through all elements in a list.
Create a pattern by using only conditional statements without loops.
Use nested loops where the outer loop iterates over rows and the inner loop iterates over columns to print the desired pattern.
Utilize recursion to generate the pattern without any loops.
Write a Python program to print a triangle pattern using loops.
for i in range(1, 6): print('*' * i)
for i in range(1, 7): print('+' * i)
for i in range(1, 5): print('o' * (i + 1))
for i in range(1, 4): print('#' * i)
What is the output?
0 1 2
End
0 1 2 End
0 1 2 End
What is the difference between while-else and for-else?
'while-else' is used for error handling, while 'for-else' is for data processing.
'while-else' executes only if the loop is interrupted, while 'for-else' runs unconditionally.
Both 'while-else' and 'for-else' are interchangeable in their usage and functionality.
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.
Provide an example of a series generated using a for loop.
987, 24, 6
2, 4, 6, 8, 10
15, 45, 136, 68, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
72, 36, 18, 9, 27, 81, 243
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?
1, 4, 9, 16, 25
2, 4, 6, 8, 10
0, 1, 2, 3, 4
100, 50, 25, 12, 6, 3, 1
What is the output?
Completed
No output
2
Error
How can you use a while loop to generate a Fibonacci series?
Initialize a, b = 1, 2;
while count < n:
print(b);
a, b = b, a + b;
Initialize a, b = 0, 1;
while count < n:
print(a);
a, b = b, a + b;
Start with a, b = 0, 1;
for i in range(n):
print(a);
a, b = b, a + b;
Set a, b = 1, 1;
while n > 0:
print(b);
b, a = a + b, b;
What is the output of the following code:
for i in range(5):
print(i) ?
5
-1
3.5
0 1 2 3 4
How do you handle infinite loops in Python?
Replace the loop with a recursive function.
Use print statements to debug the loop.
Ignore the loop and let it run indefinitely.
Use break conditions, timeouts, or exception handling to manage infinite loops.
Find Answer
55
60
50
45
What will be the output of the following code: for i in range(3): print(i * 2)?
0 1 2
0 2 4
0 2 4 6
1 2 3
How can you create a countdown using a while loop in Python?
while count > 0: print(count) count -= 1
while count >= 0: print(count) count = count + 1
while count < 0: print(count) count += 1
while count != 0: print(count) count = count - 1
What will be the result of the following code snippet? for i in range(5): if i == 3: break print(i)
0 1 2 3
1 2 3 4
0 1 2
0 1 2 3 4
Find Answer
25
30
55
20
Find answer
Prints 55
Infinite loop
Indentation Error
No output
Find answer
Prints 10 to 0
Prints 10 once
Infinite loop
Error
Find answer
Infinite loop
Value Error
Prints nothing
Syntax error
Find answer
Infinite loop
Runtime error
5
No output (logical error)
Find answer
Prints Hello once
Infinite loop
No output
Error
What will be the output?
0 1 2 3 4
2 4 6 8 10
2 3 4 5 6
5 6
What happens?
Infinite loop
Prints 0 to 4 twice
Prints 0 to 4 once
Error
Output?
5 4 3 2 1
0
No output
Error
Output?
0 1 2
Infinite loop
Error
0 1
What happens?
Infinite loop
Prints "Loop" once
Error
No output
What happens?
0 1 2
0 only
Infinite loop
Error
Output?
5 4 3 2 1 0
5 4 3 2 1
Infinite loop
Error
Output?
012
012
012
000
111
222
000
000
000
012
123
234
Output?
*
**
***
***
**
*
****
***
**
***
***
***
Output?
0
01
012
0
01
(empty line)
0
01
0
1
2
Output?
*
**
***
***
**
*
*
*
*
******
Output?
*
*
*
*
*
*
*
*
*
**
**
**
***
Output?
*
**
***
***
**
*
****
***
**
***
***
***
Output?
1 3 5
3 5 7
2 4 6
Infinite loop
Output?
1 2 3 4 5
1 2 X 4 5
X X
Error
Output?
0 1 2 3 4
4
No output
Infinite loop
Output?
0 1 2 3 4
0 1 2 3
4
No output
