Python Programming - Loops

Exercise : Loops - General Questions
  • Loops - General Questions
11.
What does the else block in a for loop execute when the loop completes without encountering a break statement?
It is executed when the loop condition is True.
It is executed when the loop is terminated prematurely.
It is executed only if an exception occurs in the loop.
It is executed when the loop completes its iterations normally.
Answer: Option
Explanation:
The else block in a for loop is executed when the loop completes its iterations normally, without encountering a break statement.

12.
In a while loop, what will happen if the loop condition is initially False?
The loop will run indefinitely.
The loop will never execute.
The loop will execute once.
An error will occur.
Answer: Option
Explanation:
If the loop condition is initially False in a while loop, the loop will never execute.

13.
What does the zip function do when used with loops?
Combines multiple sequences into tuples.
Iterates over the values of a sequence.
Reverses the order of iteration.
Checks for equality of elements in multiple sequences.
Answer: Option
Explanation:
The zip function combines multiple sequences into tuples, allowing you to iterate over corresponding elements from each sequence simultaneously.

14.
Which statement is used to iterate over a sequence and execute a block of code as long as a specified condition is True?
for
while
do-while
if
Answer: Option
Explanation:
The while loop is used to iterate over a sequence and execute a block of code as long as a specified condition is True.

15.
In Python, what is the purpose of the else block in a while loop?
It is executed when the loop is terminated prematurely.
It is executed only if an exception occurs in the loop.
It is executed when the loop condition becomes False.
It is executed when the loop condition is True.
Answer: Option
Explanation:
The else block in a while loop is executed when the loop condition becomes False.