Python Conditional Statements MCQ: Set 4

Python Conditional Statements MCQ

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

				
					num1 = 12.6
if type(num1) == float:
    print("True")
else:
    print("False")
				
			

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

Correct answer is:a) True
Explanation: The code checks if the type of `num1` is equal to `float`. Since `num1` is assigned the value `12.6`, which is a float, the condition evaluates to `True`, and “True” 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")
				
			

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.

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

				
					   str1 = 'Hello'
   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) Hello
d) olleH

Correct answer is:b) It is not a palindrome string
Explanation: The variable `str1` is assigned the string ‘Hello’. The variable `str2` is assigned the reverse of `str1`, which is ‘olleH’. Since `str1` is not equal to `str2`, the condition in the if statement is False, and the code inside the else block is executed, printing “It is not a palindrome string”.

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

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

a) True
b) False
c) The code will produce an error
d) No output

Correct answer is:b) False
Explanation: The value of `num` is -45, which is not greater than 0. Therefore, the condition `num > 0` evaluates to False, and the code will print “False”.

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

				
					char = 'c'
if char.islower():
    print("True")
else:
    print("False")
				
			

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

Correct answer is:a) True
Explanation: The `islower()` method is used to check if the given character is lowercase. In this case, the character ‘c’ is indeed lowercase, so the condition `char.islower()` evaluates to True. Therefore, the code will print “True”.

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

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

				
					for i in range(10, 16):
    if i != 13:
        print(i)
				
			

a) 10 11 12 13 14 15
b) 10 11 12 14 15
c) 11 12 13 14 15
d) 10 12 14

Correct answer is:b) 10 11 12 14 15
Explanation: The code uses a `for` loop to iterate over the range from 10 to 15 (inclusive). For each value of `i`, it checks if `i` is not equal to 13. If `i` is not equal to 13, it prints the value of `i`. Since the condition `i != 13` is `True` for all values except 13, the output will be 10, 11, 12, 14, and 15.

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

				
					year = 2000
if (year%100 != 0 or year%400 == 0) and year%4 == 0:
    print("The given year is a leap year.")
else:
    print("The given year is not a leap year.")
				
			

a) The given year is a leap year.
b) The given year is not a leap year.
c) Error: Invalid syntax.
d) Error: IndentationError.

Correct answer is:a) The given year is a leap year.
Explanation: In this code, the given year is 2000. The condition `(year%100 != 0 or year%400 == 0) and year%4 == 0` evaluates to `True` because 2000 is divisible by 4 and 400. Therefore, the code will print “The given year is a leap year.”

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

				
					   str1 = "Hello"
   if type(str1) == str:
       print("True")
   else:
       print("False")
				
			

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

Correct answer is:a) True
Explanation: The code checks if the type of `str1` is equal to `str`. Since `str1` is assigned a string value “Hello”, which is of type `str`, the condition is True, and “True” will be printed.

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

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

				
					   num = 6
   if num % 2 == 0 and num % 3 == 0:
       print("FizzBuzz")
   elif num % 2 == 0:
       print("Fizz")
   elif num % 3 == 0:
       print("Buzz")
				
			

a) FizzBuzz
b) Fizz
c) Buzz
d) No output

Correct answer is:a) FizzBuzz
Explanation: The condition num % 2 == 0 is True because 6 is divisible by 2. The condition num % 3 == 0 is also True because 6 is divisible by 3. Since both conditions are True, the code will print “FizzBuzz”.

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

				
					month = "February"

if month == "january":
    print("Number of days: 31")
elif month == "february":
    print("Number of days: 28/29")
elif month == "march":
    print("Number of days: 31")
elif month == "april":
    print("Number of days: 30")
elif month == "may":
    print("Number of days: 31")
elif month == "june":
    print("Number of days: 30")
elif month == "july":
    print("Number of days: 31")
elif month == "august":
    print("Number of days: 31")
elif month == "september":
    print("Number of days: 30")
elif month == "october":
    print("Number of days: 31")
elif month == "november":
    print("Number of days: 30")
elif month == "december":
    print("Number of days: 31")
else:
    print("Invalid month")
				
			

a) Number of days: 28/29
b) Number of days: 30
c) Number of days: 31
d) Invalid month

Correct answer is:a) Number of days: 28/29
Explanation: In this code, the variable `month` is set to “February”. Since the value of `month` matches the string “february” in lowercase, the second `elif` condition is True. Therefore, the statement `print(“Number of days: 28/29”)` will be executed, indicating that February has 28 or 29 days in a leap year.

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

				
					s1 = 15
s2 = 15
s3 = 15

if s1 == s2 == s3:
    print("It is an equilateral triangle")
else:
    print("It is not an equilateral triangle")

				
			

a) It is an equilateral triangle
b) It is not an equilateral triangle
c) None of the above

Correct answer is:a) It is an equilateral triangle
Explanation: The code checks if all three sides of the triangle are equal using the equality comparison operator (==). Since s1, s2, and s3 are all equal to 15, the condition evaluates to True. Therefore, the statement “It is an equilateral triangle” is printed.

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

				
					month = "June"
if month == "February" or month == "March" or month == "April" or month == "May":
    print("Summer")
elif month == "June" or month == "July" or month == "August" or month == "September":
    print("Rainy")
else:
    print("Winter")
				
			

a) Summer
b) Rainy
c) Winter
d) No output

Correct answer is:b) Rainy
Explanation: The variable `month` is assigned the value “June”. Since the condition `month == “June” or month == “July” or month == “August” or month == “September”` is true, the code block under the `elif` statement will execute, and “Rainy” will be printed.

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

				
					char = 'A'
vowel = ["A","E","I","O","U","a","e","i","o","u"]

if char in vowel:
    print("True")
else:
    print("False")
				
			

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

Correct answer is:a) True
Explanation: The character ‘A’ is present in the list vowel, which contains all the uppercase and lowercase vowels. Therefore, the condition char in vowel evaluates to True, and “True” is printed.

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

				
					marks = 90

if 85 <= marks <101:
    print("Stream alloted: Science")
elif 70 <= marks < 85:
    print("Stream alloted: Commerce")
elif 35 <= marks < 70:
    print("Arts")
elif 0 < marks < 35:
    print("Stream alloted: Fail")
else:
    print("Invalid marks")
				
			

a) Stream alloted: Science
b) Stream alloted: Commerce
c) Arts
d) Invalid marks

Correct answer is: a) Stream alloted: Science
Explanation: The value of marks falls within the range of 85 to 101, satisfying the condition 85 <= marks < 101. Therefore, the code will print “Stream alloted: Science”.

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 snippet?

				
					num = 69
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:b) The given number is positive and odd
Explanation: The number `num` is 69, which is greater than 0. The first condition `num>0` is True. Then, we check if `num` is divisible by 2 (`num%2 == 0`). In this case, it is not divisible by 2. Therefore, the code prints “The given number is positive and odd”.

19). What is the output of the following code when `num` is initialized as -24?

				
					num = -24
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:c) The given number is negative and even
Explanation: Since `num` is assigned the value -24, it is less than 0. Therefore, the code executes the else block. The condition `num%2 == 0` is False because -24 is an even number. Hence, the output will be “The given number is negative and even”.

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

				
					num1 = 28
num2 = 13

if num1 == num2:
    print("The given numbers are equal")
else:
    print("The given numbers are not equal")
				
			

a) The given numbers are equal
b) The given numbers are not equal
c) Error: Invalid syntax
d) No output

Correct answer is:b) The given numbers are not equal
Explanation: In the code, `num1` is assigned the value 28, and `num2` is assigned the value 13. The `if` condition `num1 == num2` checks if `num1` is equal to `num2`. Since 28 is not equal to 13), the condition evaluates to False, and the `else` block is executed. Hence, the output will be “The given numbers are not equal”.

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

				
					   num = 5 + 6j
   if type(num) == complex:
       print("True")
   else:
       print("False")
				
			

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

Correct answer is:a) True
Explanation: The code checks the type of the variable `num` using the `type()` function. Since `num` is a complex number (5+6j), the condition `type(num) == complex` evaluates to True, and “True” will be printed.

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

				
					length = 5
breadth = 5

if length**2 == length*breadth:
    print("It is a square")
else:
    print("It is not a square")
				
			

a) It is a square
b) It is not a square
c) Error
d) No output

Correct answer is:a) It is a square
Explanation: In this code, the length and breadth variables are both assigned a value of 5. The if condition checks whether the square of the length is equal to the product of the length and breadth. Since 5 squared (25) is equal to 5 multiplied by 5 (25), the condition evaluates to True, and the code block inside the if statement is executed. Therefore, the message “It is a square” is printed as the output.

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

				
					num = -24
if num<0:
    print("Absolute value of the number: ",abs(num))
else:
    print("Absolute value of the number: ",num)
				
			

a) Absolute value of the number: -24
b) Absolute value of the number: 24
c) -24
d) 24

Correct answer is:b) Absolute value of the number: 24
Explanation: The code checks if the variable “num” is less than 0. Since the value of “num” is -24, which is less than 0, the if condition is True. The absolute value of -24 is 24, so the output will be “Absolute value of the number: 24”.

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

				
					   s1 = 15
   s2 = 10
   s3 = 18

   if s1 != s2 != s3:
       print("It is a scalene triangle")
   else:
       print("It is not a scalene triangle")
				
			

a) It is a scalene triangle
b) It is not a scalene triangle
c) It is a scalene triangle if s1 > s2 > s3
d) It is not a scalene triangle if s1 = s2 = s3

Correct answer is:b) It is not a scalene triangle
Explanation: In the given code, the if condition checks if s1 is not equal to s2 and s2 is not equal to s3. Since s1 = 15, s2 = 10, and s3 = 18, the condition evaluates to True. However, a scalene triangle should have all sides of different lengths. Therefore, the else statement is executed and “It is not a scalene triangle” is printed.

25). What will be printed as the output of the given code?

				
					num = 117
last_digit = num%10

if last_digit%4 == 0:
    print("The last digit is divisible by 4")
else:
    print("The last digit is not divisible by 4")
				
			

a) The last digit is divisible by 4
b) The last digit is not divisible by 4
c) The code will result in an error
d) No output will be printed

Correct answer is:b) The last digit is not divisible by 4
Explanation: The code checks if the last_digit is divisible by 4 using the condition last_digit%4 == 0. If the condition is True, it prints “The last digit is divisible by 4”. Otherwise, it prints “The last digit is not divisible by 4”. Since the value of last_digit is 7, which is not divisible by 4, the output will be “The last digit is not divisible by 4”.

Leave a Comment