Python Conditional Statements MCQ: Set 3

Python Conditional Statements MCQ

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

				
					age = 20
if age >= 18:
    print("You are eligible")
else:
    print("You are not eligible")
				
			

a) You are eligible
b) You are not eligible
c) No output
d) Error

Correct answer is:a) You are eligible
Explanation: The condition `age >= 18` is True because `age` is equal to 20, which is greater than or equal to 18. Therefore, the statement `print(“You are eligible”)` is executed, resulting in the output “You are eligible”.

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

				
					num1 = 121
num2 = str(num1)

if num1 == int(num2[::-1]):
    print("It is a palindrome number")
else:
    print("It is not a palindrome number")
				
			

a) It is a palindrome number
b) It is not a palindrome number
c) 121
d) 112

Correct answer is:a) It is a palindrome number
Explanation: The code snippet initializes num1 with the value 121 and converts it to a string num2. It then checks whether num1 is equal to its reverse using slicing (num2[::-1]). Since the reverse of 121 is also 121, the condition is True and the message “It is a palindrome number” is printed.

3). 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.

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

				
					num1 = 234
num2 = str(num1)

if num1 == int(num2[::-1]):
    print("It is a palindrome number")
else:
    print("It is not a palindrome number")
				
			

a) It is a palindrome number
b) It is not a palindrome number
c) The code will raise an error
d) There will be no output

Correct answer is:b) It is not a palindrome number
Explanation: Since num1 is not equal to its reverse, the condition num1 == int(num2[::-1]) evaluates to false. Therefore, the code will execute the else block and print “It is not a palindrome number”.

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

				
					    x = 5
    if x < 3:
        if x < 7:
            print("A")
        else:
            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 < 3 is False. The condition x < 7 is True, so “C” will be printed. The else statement is not executed.

6). 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.

7). 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.

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

				
					str1 = 'jaj'
str2 = str1[::-1]

if str1 == str2:
    print("It is a palindrome string")
else:
    print("It is not a palindrome string")
				
			

a) It is a palindrome string
b) It is not a palindrome string
c) Error: invalid syntax
d) Error: undefined variable

Correct answer is:a) It is a palindrome string
Explanation: The code defines a variable `str1` with the value ‘jaj’. Then, it creates another variable `str2` by reversing `str1`. Since `str1` is equal to its reverse (`str2`), the condition `str1 == str2` evaluates to True. As a result, the code will print “It is a palindrome string”.

9). 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")
				
			

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

Correct answer is:b) B
Explanation: The condition x < 3 is False. The condition x < 7 is True, so “B” will be printed. The elif condition is not checked because the first if condition is True.

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

				
					marks = 50
if marks >= 45:
    print("Pass")
else:
    print("Fail")
				
			

a) Pass
b) Fail
c) Error
d) No output

Correct answer is:a) Pass
Explanation: The variable `marks` has a value of 50. The condition `marks >= 45` evaluates to True because 50 is greater than or equal to 45. Therefore, the code inside the if block is executed and “Pass” is printed.

11). 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.

12). 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.

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

				
					   num = 52
   if num > 0:
       print("True")
   else:
       print("False")
				
			


a) True
b) False
c) Error
d) No output

Correct answer is:a) True
Explanation: The condition num > 0 is True because 52 is greater than 0. Therefore, the code will print “True”.

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

				
					num = 50
if num > 0:
    if num % 2 == 0:
        print("The given number is positive and even")
    else:
        print("The given number is positive and odd")
else:
    if num % 2 == 0:
        print("The given number is negative and even")
    else:
        print("The given number is negative and odd")
				
			

a) The given number is positive and even
b) The given number is positive and odd
c) The given number is negative and even
d) The given number is negative and odd

Correct answer is:a) The given number is positive and even
Explanation: The number `num` is 50, which is a positive number. It is also divisible by 2, so the condition `num % 2 == 0` is True. Hence, the code block under the nested if statement is executed, and “The given number is positive and even” is printed.

15). 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.

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

				
					num1 = 54
num2 = 21

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

a) 54 is greatest
b) 21 is greatest
c) num1 is greatest
d) num2 is greatest

Correct answer is:a) 54 is greatest
Explanation: In the given code, `num1` is assigned the value 54 and `num2` is assigned the value 21. The `if` condition compares `num1` and `num2` using the greater than operator (`>`). Since `num1` (54) is greater than `num2` (21), the condition evaluates to `True`. As a result, the code inside the `if` block is executed, which prints `”54 is greatest”`.

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

				
					char = 'A'
if char.isupper():
    print("True")
else:
    print("False")
				
			

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

Correct answer is:a) True
Explanation: The code checks if the character ‘A’ is uppercase using the isupper() method. Since ‘A’ is indeed an uppercase letter, the condition is True, and “True” is printed.

18). 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.

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

				
					   num1 = 54
   if type(num1) == int:
       print("True")
   else:
       print("False")
				
			

a) True
b) False
c) Error
d) None of the above

Correct answer is:a) True
Explanation: The code checks if the type of `num1` is equal to `int`. Since `num1` is assigned the value `54` which is an integer, the condition is True, and “True” will be printed.

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

				
					    x = 10
    if x < 5:
        print("A")
    elif x > 15:
        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 < 5 is False. The condition x > 15 is also False. The condition x > 7 is True, so “C” will be printed.

21). 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.

22). 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.

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

				
					
    x = 5
    if x < 3:
        if x < 7:
            print("A")
        else:
            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 < 3 is False. The condition x < 7 is True, so “C” will be printed. The else statement is not executed.

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

				
					num = -10
if num > 0:
    if num % 2 == 0:
        print("The given number is positive and even")
    else:
        print("The given number is positive and odd")
else:
    if num % 2 == 0:
        print("The given number is negative and even")
    else:
        print("The given number is negative and odd")
				
			

a) The given number is positive and even
b) The given number is positive and odd
c) The given number is negative and even
d) The given number is negative and odd

Correct answer is:d) The given number is negative and odd
Explanation: The code first checks if the number is greater than 0. Since `num` is -10, the condition is False. Then, it checks if the number is divisible by 2 to determine if it’s even or odd. Since -10 is not divisible by 2, the condition `num % 2 == 0` is False, and the code executes the else block, printing “The given number is negative and odd.”

25). 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.

Leave a Comment