Python Programming - Loops

Exercise : Loops - General Questions
  • Loops - General Questions
6.
Which of the following statements accurately describes the behavior of the else block in a Python while loop?
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 condition becomes False.
Answer: Option
Explanation:
The else block in a while loop is executed when the loop condition becomes False.

7.
What is the purpose of the zip function in Python when used with loops?
Counts the total number of iterations
Combines multiple sequences into tuples
Returns the index and value of each element in a sequence
Reverses the order of iteration
Answer: Option
Explanation:
The zip function is used to combine multiple sequences into tuples, allowing you to iterate over corresponding elements from each sequence simultaneously.

8.
Which keyword is used to exit a loop and skip the remaining iterations?
terminate
exit
break
skip
Answer: Option
Explanation:
The break keyword is used to exit a loop prematurely and skip the remaining iterations.

9.
What does the range function return in Python when used with a single argument?
A list of numbers from 0 to the specified argument (exclusive)
A list of numbers from 1 to the specified argument (inclusive)
A list of numbers from 0 to the specified argument (inclusive)
A list of numbers from 1 to the specified argument (exclusive)
Answer: Option
Explanation:
The range function with a single argument returns a sequence of numbers starting from 0 up to (but not including) the specified argument.

10.
In a for loop, which method is used to iterate over the keys of a dictionary?
keys()
items()
values()
enumerate()
Answer: Option
Explanation:
The keys() method is used to iterate over the keys of a dictionary in a for loop.