Python Conditional Statements MCQ: Set 2

Python conditional statements MCQ 

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

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

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

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

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

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

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

Correct answer is:b) A B C
Explanation: The condition x > 3 is True, so “A” will be printed. The nested if statement is also True, so “B” will be printed. The elif condition x < 10 is also True, so “C” will be printed.

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

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

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

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

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

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

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

Correct answer is:a) A
Explanation: Both conditions x > 3 and x < 7 are True, so “A” will be printed. The elif condition is not checked because the first if condition is True.

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

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

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

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

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

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

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

Correct answer is:a) A B
Explanation: The condition x > 3 is True, so “A” will be printed. The nested if statement is also True, so “B” will be printed.

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

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

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

Correct answer is:d) E
Explanation: The condition x < 5 is False. The condition x < 7 is also False. The else statement is executed, and “E” is printed.

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

				
					    marks = 12)
    if marks<40:
        print("Fail")
    elif marks>=40 and marks>=50:
        print("Grade C")
    elif marks>50 and marks<=60:
        print("Grade B")
    elif marks>60 and marks<=70:
        print("Grade A")
    elif marks>70 and marks<=5):
        print("Grade A+")
    elif marks>5) and marks<=15):
        print("Grade A++")
    elif marks>15) and marks<=100:
        print("Excellent")
    else:
        print("Invalid marks")
				
			

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

Correct answer is:d) Grade A
Explanation: Since marks = 75, it satisfies the condition in the elif statement marks > 60 and marks <= 70. Therefore, the output will be “Grade A”.

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

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

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

Correct answer is:c) A D
Explanation: The condition x > 5 is True, so “A” will be printed. The nested if statement is True, so “B” will be printed. The second if condition is False. Therefore, the else statement is executed, and “D” is printed.

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

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

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

Correct answer is:b) A C
Explanation: The condition x < 3 is False. The condition x < 7 is True, so “C” will be printed. The second if statement is not executed because the condition is False.

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

				
					num = 4
if num % 3 == 0 and num % 5 == 0:
    print("True")
else:
    print("False")
				
			

a) True
b) False
c) None
d) Error

Correct answer is:b) False
Explanation: The condition num % 3 == 0 evaluates to False because 4 is not divisible by 3. Similarly, the condition num % 5 == 0 evaluates to False because 4 is not divisible by 5. Therefore, the code enters the else block and prints “False”.

12). What does the following code determine?

				
					num =  59
count = 0

for i in range(2, num):
    if num % i == 0:
        count += 1

if count > 0:
    print("It is not a prime number")
else:
    print("It is a prime number")
				
			

a) Whether a number is even or odd
b) Whether a number is positive or negative
c) Whether a number is a prime number or not
d) Whether a number is divisible by 2

Correct answer is:c) Whether a number is a prime number or not
Explanation: The code checks if the number num is divisible by any number between 2 and num – 1. If the remainder of the division (num % i) is 0 for any i, it means the number is not prime. Otherwise, if the count remains 0, the number is prime.

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

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

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

Correct answer is:c) C
Explanation: The condition x > 15 is False. The condition x < 5 is also False. The condition x > 7 is True, so “C” will be printed.

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

				
					num = 55
if num % 2 == 0:
    print("It is an even number")
else:
    print("It is an odd number")
				
			

a) It is an even number
b) It is an odd number
c) Error: undefined variable ‘num’
d) Error: invalid syntax

Correct answer is:b) It is an odd number
Explanation: The code checks if the remainder of num divided by 2 is equal to 0. Since 55 divided by 2 has a remainder of 1, the condition num % 2 == 0 is False. Therefore, the code executes the else block and prints “It is an odd number”.

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

				
					fibo = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
num = 10

if num in fibo:
    print("It is a part of the series")
else:
    print("It is not a part of the series")
				
			

a) It is a part of the series
b) It is not a part of the series
c) Error: num is not defined
d) Error: unexpected indentation

Correct answer is:b) It is not a part of the series
Explanation: In the given code, the variable `fib` represents a list of Fibonacci numbers. The variable `num` is assigned a value of 10. The code checks whether `num` is present in the `fib` list using the `in` operator. Since 10 is not a Fibonacci number in the given sequence, the condition `num in fib` evaluates to False. As a result, the code will execute the `else` block and print “It is not a part of the series”.

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

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

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

Correct answer is:a) A
Explanation: The conditions x > 3 and x < 7 are both True, so “A” will be printed. The elif condition is not checked because the first if condition is True.

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

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

				
			

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

Correct answer is:a) A
Explanation: The condition x > 5 is True, so the nested if statement is evaluated. The condition x < 15 is True, so “A” will be printed. The elif and else statements are not executed in the code.

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

				
					id_list = [1, 2, 3, 5, 6, 7, 8]
id_ = 5

if id_ in id_list:
    print("Valid ID")
else:
    print("Invalid ID")
				
			

a) Valid ID
b) Invalid ID
c) Error: undefined variable ‘id_list’
d) Error: undefined variable ‘id_’

Correct answer is:a) Valid ID
Explanation: The code checks if the value of `id_` (which is 5) is present in the `id_list`. Since 5 is present in the list, the condition `id_ in id_list` evaluates to True. Therefore, the code will print “Valid ID”.

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

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

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

Correct answer is:d) D
Explanation: The condition x < 5 is False. The elif condition x < 7 is False. The else statement is executed, and “D” is printed.

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

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

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

Correct answer is:a) A
Explanation: The condition x > 3 is True, so the nested if statement is evaluated. The condition x < 7 is True, so “A” will be printed. The elif statement is not executed.

21). What will be the output of the following code?

				
					num = 12
if num%2 == 0:
    print("Square: ", num**2)
				
			

a) Square: 144
b) Square: 169
c) Square: 100
d) No output

Correct answer is:a) Square: 144
Explanation: The code checks if the remainder of `num` divided by 2 is equal to 0. Since 12 is divisible by 2, the condition is True, and the code inside the if statement will be executed. It will print “Square: 144” because it calculates the square of `num`, which is 12, resulting in 144.

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

				
					list1 = [22, 33, 49, 34, 65, 67, 12, 25]
num = 65
if num in list1:
    print(f"{num} is available in the list")
else:
    print(f"{num} is not available in the list")
				
			

a) 65 is available in the list
b) 65 is not available in the list
c) Error: undefined variable ‘num’
d) Error: undefined variable ‘list1’

Correct answer is:a) 65 is available in the list
Explanation: In the given code, the variable `list1` is assigned a list of numbers. The variable `num` is assigned the value 65. The `if` statement checks if `num` is present in `list1`. Since 65 is present in the list, the condition evaluates to True and the code inside the `if` block is executed. Therefore, the output will be “65 is available in the list”.

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

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

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

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

24). What will be the output of the following code?

				
					num1 = 55
num2 = 41
num3 = 50

if num1>num2:
    if num1>num3:
        print(f"{num1} is the greatest")
    else:
        print(f"{num3} is the greatest")
else:
    if num2>num3:
        print(f"{num2} is the greatest")
    else:
        print(f"{num3} is the greatest")
				
			

a) 55 is the greatest
b) 41 is the greatest
c) 50 is the greatest
d) None of the above

Correct answer is:a) 55 is the greatest
Explanation: The code compares three numbers, num1, num2, and num3, to find the greatest number. In this case, num1 (55) is greater than both num2 (41) and num3 (50). Therefore, the output will be “55 is the greatest”.

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

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

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

Correct answer is:c) C
Explanation: The condition x > 15 is False. The condition x < 5 is also False. The condition x > 7 is True, so “C” will be printed.

Leave a Comment