Python conditional statements MCQ: Set 1

Python conditional statements MCQ

1). What is the purpose of an if statement in Python?

a) To check if a condition is True or False
b) To perform a loop
c) To define a function
d) To import a module

Correct answer is: a) To check if a condition is True or False
Explanation: The if statement allows you to execute a block of code only if a specific condition is met.

2). Identify the correct syntax for an if statement?

a) if condition:
b) if condition then:
c) if (condition)
d) if (condition) then

Correct answer is: a) if condition:
Explanation: The correct syntax for an if statement in Python is “if condition:”

3). What is the purpose of the elif statement?

a) To check another condition if the if condition is False
b) To end the execution of the program
c) To execute a block of code repeatedly
d) To define a variable

Correct answer is: a) To check another condition if the if condition is False
Explanation: The elif statement allows you to check an additional condition if the if condition is False.

4). Which keyword is used to denote the else statement in Python?

a) otherwise
b) ifnot
c) else
d) elseif

Correct answer is:c) else
Explanation: The else keyword is used to denote the else statement in Python.

5). What is the purpose of the else statement?

a) To define a variable
b) To end the execution of the program
c) To execute a block of code if all previous conditions are False
d) To import a module

Correct answer is:c) To execute a block of code if all previous conditions are False
Explanation: The else statement allows you to execute a block of code if none of the previous conditions are True.

6). Which of the following is the correct syntax for an if-else statement?

a) if condition: else:
b) if (condition) else:
c) if condition: else
d) if (condition): else:

Correct answer is:a) if condition: else:
Explanation: The correct syntax for an if-else statement in Python is “if condition: else:”

7). What is the output of the following code?

				
					
   x = 5
   if x > 10:
       print("A")
   elif x > 7:
       print("B")
   elif x > 3:
       print("C")
   else:
       print("D")
				
			

Correct answer is:c) C
Explanation: The condition x > 3 is True, so the output will be “C”.

8). What happens if there are multiple elif statements and more than one condition is True?

a) Only the first True condition is executed
b) All True conditions are executed
c) None of the True conditions are executed
d) An error is raised

Correct answer is:a) Only the first True condition is executed
Explanation: Once a True condition is found, the corresponding block of code is executed, and the rest of the conditions are skipped.

9). Which of the following is the correct way to nest if statements in Python?

a) if condition1: if condition2:
b) if condition1: elif condition2:
c) if condition1: else: if condition2:
d) if condition1: if condition2: else:

Correct answer is:a) if condition1: if condition2:
Explanation: You can nest if statements by placing one if statement inside another if statement.

10). What is the output of the following code?

				
					    x = 10
    if x > 5:
        print("A")
    if x > 7:
        print("B")
    if x > 15:
        print("C")
    else:
        print("D")
				
			

a) A B C D
b) A B D
c) A D
d) B D
Correct answer is:b) A B D
Explanation: The conditions x > 5 and x > 7 are True, so “A” and “B” will be printed. The condition x > 15 is False, so “D” will be printed.

11). Which logical operator is used to combine multiple conditions in an if statement?

a) &&
b) ||
c) ^
d) and

Correct answer is: d) and
Explanation: The logical operator “and” is used to combine multiple conditions in an if statement.

12). What is the output of the following code?

				
					    x = 5
    y = 10
    if x > 3 and y < 15:
        print("A")
    else:
        print("B")
				
			

a) A
b) B
c) A B
d) No output

Correct answer is:a) A
Explanation: Both conditions x > 3 and y < 15 are True, so “A” will be printed.

13). What is the output of the following code?

				
					    x = 5
    y = 10
    if x > 3 or y < 5:
        print("A")
    else:
        print("B")
				
			

a) A
b) B
c) A B
d) No output

Correct answer is:b) B
Explanation: Only the condition x > 3 is True, but the condition y < 5 is False. Since the logical operator “or” requires at least one True condition, “B” will be printed.

14). What is the output of the following code?

				
					    x = 7
    if x > 5:
        print("A")
        if x > 10:
            print("B")
    else:
        print("C")
				
			

a) A
b) A B
c) A C
d) No output

Correct answer is:a) A
Explanation: The condition x > 5 is True, so “A” will be printed. The nested if statement is skipped because the condition x > 10 is False.

15). Which of the following is the correct way to write an if-else statement without using elif?

a) if condition: then
b) if condition: else
c) if condition: else then
d) if condition: else:

Correct answer is:d) if condition: else:
Explanation: The correct syntax for an if-else statement without using elif is “if condition: else:”

16). What is the output of the following code?

				
					    x = 5
    if x > 3:
        print("A")
    elif x > 5:
        print("B")
    else:
        print("C")
				
			

a) A
b) B
c) C
d) No output

Correct answer is:a) A
Explanation: The condition x > 3 is True, so “A” will be printed. The condition x > 5 is not checked because the first if condition is True.

17). How many levels of nested if statements are allowed in Python?

a) 1
b) 2
c) 3
d) No limit

Correct answer is:d) No limit
Explanation: Python allows you to nest if statements as many times as needed.

18). What is the output of the following code?

				
					    x = 10
    if x > 5:
        if x < 15:
            print("A")
        else:
            print("B")
    else:
        print("C")
				
			

a) A
b) B
c) A B
d) No output

Correct answer is:a) A
Explanation: Both conditions x > 5 and x < 15 are True, so “A” will be printed. The else statement is skipped.

19). What is the output of the following code?

				
					
    x = 5
    if x > 3:
        print("A")
    if x > 5:
        print("B")
    else:
        print("C")
				
			

a) A
b) B
c) A B
d) A C

Correct answer is:c) A B
Explanation: The condition x > 3 is True, so “A” will be printed. The condition x > 5 is False, so “B” will not be printed. The else statement is skipped.

20). What is the output of the following code?

				
					    x = 10
    if x > 15:
        print("A")
    elif x > 10:
        print("B")
    else:
        print("C")
				
			

a) A
b) B
c) C
d) No output

Correct answer is:c) C
Explanation: The condition x > 15 is False. The condition x > 10 is also False. Therefore, the else statement is executed, and “C” is printed.

21). What is the output of the following code?

				
					    x = 5
    if x < 3:
        print("A")
    elif x < 7:
        print("B")
    elif x < 10:
        print("C")
    else:
        print("D")
				
			

a) A
b) B
c) C
d) D

Correct answer is:b) B
Explanation: The condition x < 7 is True, so “B” will be printed. The elif condition x < 10 is also True, but only the first True condition is executed.

22). What is the output of the following code?

				
					    x = 10
    if x > 5:
        print("A")
        if x > 15:
            print("B")
        else:
            print("C")
    else:
        print("D")
				
			

a) A B
b) A C
c) A C D
d) No output

Correct answer is:b) A C
Explanation: The condition x > 5 is True, so “A” will be printed. The nested if statement is skipped because the condition x > 15 is False. The else statement is executed, and “C” is printed.

23). What is the output of the following code?

				
					    x = 10
    if x > 5:
        print("A")
    if x > 15:
        print("B")
    else:
        print("C")
				
			

a) A
b) B
c) A C
d) C

Correct answer is:a) A C
Explanation: The condition x > 5 is True, so “A” will be printed. The condition x > 15 is False, so “B” will not be printed. The else statement is executed, and “C” is printed.

24). What is the output of the following code?

				
					    num = 5 
    if num%3 == 0:
        print("Number is divisible by 3")
    else:
        print("Number is not divisible by 3")
				
			

a) Number is divisible by 3
b) Number is not divisible by 3
c) SyntaxError
d) ValueError

Correct answer is:b) Number is not divisible by 3
Explanation: The condition num % 3 == 0 is False because 5 divided by 3 leaves a remainder of 2. Therefore, the code will print “Number is not divisible by 3”.

25). What is the purpose of the following code?

				
					    for i in range(1, 31):
        if i % 3 == 0:
            print(i, end=" ")

				
			

a) To print all numbers from 1 to 30
b) To print all multiples of 3 from 1 to 30
c) To print all even numbers from 1 to 30
d) To print all odd numbers from 1 to 30

Correct answer is:b) To print all multiples of 3 from 1 to 30
Explanation: The code uses a for loop to iterate through numbers in the range from 1 to 30.

Leave a Comment