Exercise: Loops

Questions for: Loops

What is the purpose of the sorted function in Python when used with loops?
A:
Reverses the order of elements in a list.
B:
Sorts the elements in ascending order.
C:
Returns the index and value of each element in a sequence.
D:
Checks for equality of elements in multiple sequences.
Answer: B
The sorted function in Python is used to sort the elements in ascending order.
Which of the following statements accurately describes the behavior of the else block in a Python try-except statement?
A:
It is executed when an exception occurs in the try block.
B:
It is executed when the try block completes without any exceptions.
C:
It is executed only if an exception occurs in the except block.
D:
It is executed when the program terminates.
Answer: B
The else block in a try-except statement is executed when the try block completes without any exceptions.
Which of the following is the correct syntax for a do-while loop?
A:
do { } while condition;
B:
while condition: { }
C:
do: 
 code 
while condition
D:
while True: 
    code 
    if not condition: break
Answer: D
Python does not have a built-in do-while loop, but it can be simulated using a while loop with a break statement to exit the loop based on a condition.
In Python, what does the enumerate function return when used with a for loop?
A:
Index and value of each element in a sequence
B:
Values of a sequence
C:
Reversed order of elements in a sequence
D:
Total number of iterations
Answer: A
The enumerate function returns the index and value of each element in a sequence when used with a for loop.
What is the purpose of the reverse parameter in the sorted function?
A:
It reverses the order of elements in a list.
B:
It creates a reversed range from a specified end to start.
C:
It reverses the order of iteration in a loop.
D:
It sorts the elements in descending order.
Answer: D
The reverse parameter in the sorted function is used to sort the elements in descending order.
Ad Slot (Above Pagination)
Quiz