Exercise: Loops
Questions for: Loops
In a
while loop, what will happen if the loop condition is initially False?
A:
The loop will run indefinitely.
B:
The loop will never execute.
C:
The loop will execute once.
D:
An error will occur.
Answer: B
If the loop condition is initially
False in a while loop, the loop will never execute.
What does the
else block in a for loop execute when the loop completes without encountering a break statement?
A:
It is executed when the loop condition is True.
B:
It is executed when the loop is terminated prematurely.
C:
It is executed only if an exception occurs in the loop.
D:
It is executed when the loop completes its iterations normally.
Answer: D
The
else block in a for loop is executed when the loop completes its iterations normally, without encountering a break statement.Discuss About this Question.
In a for loop, which method is used to iterate over the keys of a dictionary?
A:
keys()
B:
items()
C:
values()
D:
enumerate()
Answer: A
The
keys() method is used to iterate over the keys of a dictionary in a for loop.Discuss About this Question.
What does the
range function return in Python when used with a single argument?
A:
A list of numbers from 0 to the specified argument (exclusive)
B:
A list of numbers from 1 to the specified argument (inclusive)
C:
A list of numbers from 0 to the specified argument (inclusive)
D:
A list of numbers from 1 to the specified argument (exclusive)
Answer: A
The
range function with a single argument returns a sequence of numbers starting from 0 up to (but not including) the specified argument.Discuss About this Question.
Which keyword is used to exit a loop and skip the remaining iterations?
A:
terminate
B:
exit
C:
break
D:
skip
Answer: C
The
break keyword is used to exit a loop prematurely and skip the remaining iterations.Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.