Question 1
What is the purpose of the else clause in a Python if-else statement?
It is used to handle exceptions.
It is executed when the if condition is true.
It is executed when none of the preceding if or elif conditions are true.
It is used to terminate the program.
Question 2
What is the purpose of the elif keyword in a Python if-elif-else statement?
It is short for "else if" and is used to check multiple conditions sequentially.
It is used to terminate the program if the if condition is false.
It is used to declare a variable.
It is executed when the if condition is true.
Question 3
Which of the following statements is true regarding the if-else statement in Python?
The else block is mandatory.
The if block is optional.
Either the if block or the else block will be executed, but not both.
Both the if and else blocks can be executed under certain conditions.
Question 4
Q4-What is the purpose of the range() function in a Python for loop?
A. To generate a list of numbers.
B. To specify the number of iterations for the loop.
C. To create a sequence of numbers over which the loop will iterate.
D. To calculate the average of the loop variables.
Question 5
What is the output of the following code?
for i in range(1, 4):
print(i)
1 2 3
1 2 3 4
0 1 2
Error
Question 6
Q6-In a Python for loop, which method is used to iterate over the elements of an iterable (e.g., a list or a string) and retrieve both the index and the value?
A. enumerate()
B. iter()
C. range()
D. zip()
Question 7
Q7-What is the primary purpose of a while loop in Python?
A. To define a function.
B. To create an infinite loop.
C. To iterate over a sequence of elements.
D. To repeatedly execute a block of code as long as a condition is true.
Question 8
Q8-Consider the following code. What will be the output?
x = 5
for i in range(1, x + 1):
if x % i == 0:
print(i, end=" ")
1 2 3 5
1 5
5
1 2 3 4 5
Question 9
In a while loop, when is the condition checked?
Before each iteration.
After each iteration.
Only at the beginning.
Only at the end.
Question 10
Q10-What is the purpose of the else clause in a while loop?
A. It is executed if the loop encounters an error.
B. It is executed when the loop variable reaches its maximum value.
C. It is executed when the loop condition becomes false.
D. It is used to define an alternative condition for the loop.
There are 10 questions to complete.