50 MCQ on if else in Python
MCQ on if else
Question .1 Which one of the following is a valid Python if statement :
Option a.) if a>=2 :
Option b.) if (a >= 2)
Option c.) if (a => 22)
Option d.) if a >= 22
Answer
#Answer: a
Question .2 Which of following is not a decision-making statement.
Option a.) if-elif statement
b. for statement
c. if -else statement
d. if statement
Answer
#Answer: b
Question .3 What does the following code print to console.
if True:
print(1001)
else:
print(2002)
Option a.) 1001
b.true
c.2002
d. false
Answer
#Answer: a
Question .4 What will be the output of the following Python code?
list1 = [3 , 2 , 5 , 6 , 0 , 7, 9]
sum = 0
sum1 = 0
for elem in list1:
if (elem % 2 == 0):
sum = sum + elem
continue
if (elem % 3 == 0):
sum1 = sum1 + elem
print(sum , end=" ")
print(sum1)
a. ‘8’ 9
b. 8 3
c. 2 3
d. 8 12
Answer
#Answer: D
Question .5 What will be the output of the following Python code?
if 4+5==10:
print("TRUE")
else:
print("False")
print ("True")
a. False
True
b. True
True
c. false
d. none
Answer
#Answer: A
Question .6 What keyword would you use to add an alternative condition to an if statement?
a. else if
b. elseif
c. elif
d. None of the above
Answer
#Answer: C
Question .7 What will be the output of the following Python code?
str1="learn python"
str2=""
str3=""
for x in str1:
if(x=="r" or x=="n" or x=="p"):
str2+=x
pass
if(x=="r" or x=="e" or x=="a"):
str3+=x
print(str2,end=" ")
print(str3)
Option a.) rnpn ea
Option b.) rnpn ear
Option c.) rnp ea
Option d.) rnp ear
Answer
#Answer: B
Question .8 Predict the output of the following code:
X=3
if x>2 or x<5 and x==6:
print("ok")
else:
print("no output")
a . ok
b. okok
c. no output
d. none of above
Answer
#Answer: C
Question .9 Predict the output of the following code:
x,y=2,4
if(x+y= =10):
print("true")
else:
print("false")
a. true
b .false
c. no output
d. none
Answer
#Answer: B
Question .10 Consider the following code segment:
a = int(input("Enter an integer: "))
b = int(input("Enter an integer: "))
if a <= 0:
b = b +1
else:
a = a + 1
if a > 0 and b > 0:
print ("W")
elif a > 0:
print("X")
if b > 0:
print("Y")
else:
print("Z")
What letters will be printed if the user enters -1 for a and -1 for b?
Option a.) Only W
b. Only X
c. Only Y
d. Only Z
Answer
#Answer: D