Python Programming Multiple Choice Question -
Conditionals And Loops
This section focuses on the "Conditionals And Loops" of the Python programming. These Multiple Choice Questions (mcq) should be practiced to improve the Python programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Which of the following is not used as loop in Python?
A. for loop
B. while loop
C. do-while loop
D. None of the above
View Answer
Ans : C
Explanation: do-while loop is not used as loop in Python.
2. Which of the following is False regarding loops in Python?
A. Loops are used to perform certain tasks repeatedly.
B. While loop is used when multiple statements are to executed repeatedly until the given condition becomes False
C. While loop is used when multiple statements are to executed repeatedly until the given condition becomes True.
D. for loop can be used to iterate through the elements of lists.
View Answer
Ans : B
Explanation: While loop is used when multiple statements are to executed repeatedly until the given condition becomes False statement is False regarding loops in Python.
3. Which of the following is True regarding loops in Python?
A. Loops should be ended with keyword "end".
B. No loop can be used to iterate through the elements of strings.
C. Keyword "break" can be used to bring control out of the current loop.
D. Keyword "continue" is used to continue with the remaining statements inside the loop.
View Answer
Ans : C
Explanation: Keyword break can be used to bring control out of the current loop statement is True regarding loops in Python.
4. What will be the output of given Python code?
n=7
c=0
while(n):
if(n>5):
c=c+n-1
n=n-1
else:
break
print(n)
print(c)
A. 5 11
B. 5 9
C. 7 11
D. 5 2
View Answer
Ans : A
Explanation: 5 11 will be the output of the given code
5. What will be the output of given Python code?
str1="hello"
c=0
for x in str1:
if(x!="l"):
c=c+1
else:
pass
print(c)
A. 2
B. 0
C. 4
D. 3
View Answer
Ans : D
Explanation: 3 will be the output of given Python code.
6. Which of the following Python code will give different output from the others?
A. for i in range(0,5):
print(i)
B. for j in [0,1,2,3,4]:
print(j)
C. for k in [0,1,2,3,4,5]:
print(k)
D. for l in range(0,5,1):
print(l)
View Answer
Ans : C
Explanation: Option C python code will give different output from the others.
7. How many times will the loop run?
i=2
while(i>0):
i=i-1
A. 2
B. 3
C. 1
D. 0
View Answer
Ans : A
Explanation: The loop will run 2 times.
8. 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
View Answer
Ans : D
Explanation: The output of the following python code is 8 12.
9. 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)
A. rnpn ea
B. rnpn ear
C. rnp ea
D. rnp ear
View Answer
Ans : B
Explanation: The output of the following python code is rnpn ear.
10. What will be the output of the following Python code?
for i in range(0,2,-1):
print("Hello")
A. Hello
B. Hello Hello
C. No Output
D. Error
View Answer
Ans : C
Explanation: There will be no output of the following python code.
11. Which one of the following is a valid Python if statement :
A. if a>=2 :
B. if (a >= 2)
C. if (a => 22)
D. if a >= 22
View Answer
Ans : A
Explanation: If statment always ended with colon (:). So, option A is correct.
12. 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
View Answer
Ans : C
Explanation: elif is used to add an alternative condition to an if statement. So, option C is correct.
13. Can we write if/else into one line in python?
A. Yes
B. No
C. if/else not used in python
D. None of the above
View Answer
Ans : A
Explanation: Yes, we can write if/else in one line. For eg i = 5 if a > 7 else 0. So, option A is correct.
14. In a Python program, a control structure:
A. Defines program-specific data structures
B. Directs the order of execution of the statements in the program
C. Dictates what happens before the program starts and after it terminates
D. None of the above
View Answer
Ans : B
Explanation: Control structures determine which statements in the program will be executed and in what order, allowing for statements to be skipped over or executed repeatedly. So, option B is correct.
15. What will be output of this expression:
'p' + 'q' if '12'.isdigit() else 'r' + 's'
A. pq
B. rs
C. pqrs
D. pq12
View Answer
Ans : A
Explanation: If condition is true so pq will be the output. So, option A is correct.
16. Which statement will check if a is equal to b?
A. if a = b:
B. if a == b:
C. if a === c:
D. if a == b
View Answer
Ans : B
Explanation: if a == b: statement will check if a is equal to b. So, option B is correct.
17. Does python have switch case statement?
A. True
B. False
C. Python has switch statement but we can not use it.
D. None of the above
View Answer
Ans : B
Explanation: Python does not have switch case statement. So, option B is correct.
18. Which of the following is a valid for loop in Python?
A. for(i=0; i < n; i++)
B. for i in range(0,5):
C. for i in range(0,5)
D. for i in range(5)
View Answer
Ans : B
Explanation: For statment always ended with colon (:). So, option B is correct.
19. Which of the following sequences would be generated bt the given line of code?
range (5, 0, -2)
A. 5 4 3 2 1 0 -1
B. 5 4 3 2 1 0
C. 5 3 1
D. None of the above
View Answer
Ans : C
Explanation: The initial value is 5 which is decreased by 2 till 0 so we get 5, then 2 is decreased so we get 3 then the same thing repeated we get 1 and now when 2 is decreased we get -1 which is less than 0 so we stop and hence we get 5 3 1. So, option C is correct.
20. A while loop in Python is used for what type of iteration?
A. indefinite
B. discriminant
C. definite
D. indeterminate
View Answer
Ans : A
Explanation: A while loop implements indefinite iteration, where the number of times the loop will be executed is not specified explicitly in advance. So, option A is correct.
21. When does the else statement written after loop executes?
A. When break statement is executed in the loop
B. When loop condition becomes false
C. Else statement is always executed
D. None of the above
View Answer
Ans : B
Explanation: Else statement after loop will be executed only when the loop condition becomes false. So, option B is correct.
22. What will be the output of the following code?
x = "abcdef"
i = "i"
while i in x:
print(i, end=" ")
A. a b c d e f
B. abcdef
C. i i i i i.....
D. No Output
View Answer
Ans : D
Explanation: There will be no output since there is no i in the string x. So, option D is correct.
23. What will be the output of the following code?
x = "abcd"
for i in range(len(x)):
print(i)
A. abcd
B. 0 1 2 3
C. 1 2 3 4
D. a b c d
View Answer
Ans : B
Explanation: len(x) will give 4 and the loop will run for 4 times starting from 0. Hence output will be 0 1 2 3. So, option B is correct.
24. What will be the output of the following code?
x = 12
for i in x:
print(i)
A. 12
B. 1 2
C. Error
D. None of the above
View Answer
Ans : C
Explanation: Objects of type int are not iterable. So, option C is correct.
25. A loop becomes infinite loop if a condition never becomes ________.
A. TRUE
B. FALSE
C. Null
D. Both A and C
View Answer
Ans : B
Explanation: A loop becomes infinite loop if a condition never becomes FALSE. You must use caution when using while loops because of the possibility that this condition never resolves to a FALSE value. This results in a loop that never ends. Such a loop is called an infinite loop.
26. If the else statement is used with a while loop, the else statement is executed when the condition becomes _______.
A. TRUE
B. FALSE
C. Infinite
D. Null
View Answer
Ans : B
Explanation: If the else statement is used with a while loop, the else statement is executed when the condition becomes false.
27. Python programming language allows to use one loop inside another loop known as?
A. switch
B. foreach
C. nested
D. forall
View Answer
Ans : C
Explanation: Python nested loops : Python programming language allows to use one loop inside another loop.
28. The ________ statement is a null operation.
A. break
B. exit
C. return
D. pass
View Answer
Ans : D
Explanation: The pass statement is a null operation; nothing happens when it executes.
29. The continue statement can be used in?
A. while loop
B. for loop
C. do-while
D. Both A and B
View Answer
Ans : D
Explanation: The continue statement can be used in both while and for loops.
Also check :
Discussion
Chenna Kesav Movva
2nd ans is B
Chenna Kesav Movva
but ig the answer should be c