Python Control Structures – 100 MCQs (CBSE NCERT 083 Computer Science)
1. What will be the output of the following code?
```python
x=5
if x > 3:
print("Yes")
else:
print("No")
```
A. Yes
B. No
C. Error
D. Nothing
✔ Answer: A
2. Which of these is the correct syntax for a while loop?
A. while x > 0
B. while x > 0:
C. while (x > 0)
D. while x > 0 then
✔ Answer: B
3. What is the output of this code?
```python
for i in range(3):
print(i)
```
A. 1 2 3
B. 0 1 2
C. 0 1 2 3
D. Error
✔ Answer: B
4. Which statement is used to skip the current iteration in a loop?
A. break
B. continue
C. pass
D. exit
✔ Answer: B
5. What is printed by this code?
```python
for i in range(1, 5):
if i == 3:
break
print(i)
```
A. 1 2 3
B. 1 2
C. 1 2 4
D. 1 2 3 4
✔ Answer: B
6. Which of these will cause an infinite loop?
A. while True:
B. for i in range(1, 5):
C. if True:
D. while x < 5:
✔ Answer: A
7. What is wrong with this code?
```python
x=5
if x > 3
print("Yes")
```
A. No error
B. Missing colon at the end of if statement
C. Incorrect indentation
D. Incorrect operator
✔ Answer: B
8. What is the output of this code?
```python
x=0
while x < 3:
print(x)
x += 1
```
A. 0 1 2
B. 1 2 3
C. 0 1 2 3
D. Error
✔ Answer: A
9. What is printed?
```python
for i in range(0, 5, 2):
print(i)
```
A. 0 1 2 3 4
B. 0 2 4
C. 1 3 5
D. 2 4
✔ Answer: B
10. Which of these is used to exit a loop?
A. stop
B. break
C. continue
D. pass
✔ Answer: B
11. What will this output?
```python
for i in range(3):
print(i)
else:
print("Done")
```
A. 0 1 2 Done
B. 0 1 2
C. Done only
D. Error
✔ Answer: A
12. Which of the following is correct to check if x is between 5 and 10?
A. if x > 5 and x < 10:
B. if x > 5 or x < 10:
C. if 5 < x < 10:
D. Both A and C
✔ Answer: D
13. What is the output?
```python
x=5
y = 10
if x > y:
print("x is greater")
else:
print("y is greater")
```
A. x is greater
B. y is greater
C. Error
D. None
✔ Answer: B
14. What will this print?
```python
x=2
if x == 1:
print("One")
elif x == 2:
print("Two")
else:
print("Other")
```
A. One
B. Two
C. Other
D. Error
✔ Answer: B
15. What happens when the else part is removed from an if statement?
A. Syntax error
B. The code will not run
C. It will still work fine
D. It will always throw an exception
✔ Answer: C
16. What is wrong with this code?
```python
for i in range(3):
print(i)
```
A. range is incorrect
B. Missing indentation
C. Syntax error in print
D. No error
✔ Answer: B
17. What is the output of this?
```python
for i in range(2):
for j in range(2):
print(i, j)
```
A. 0 0 1 1
B. 0 0 0 1 1 0 1 1
C. 0 0 0 1 1 0 1 1
D. 0 0 0 1 1 0 1
✔ Answer: B
18. What will be printed?
```python
x=0
while x < 3:
x += 1
if x == 2:
continue
print(x)
```
A. 1 2 3
B. 1 3
C. 2 3
D. 1
✔ Answer: B
19. What is the output?
```python
x=5
if x < 3:
print("Low")
elif x < 7:
print("Medium")
else:
print("High")
```
A. Low
B. Medium
C. High
D. Error
✔ Answer: B
20. What is the output of this?
```python
for i in range(1, 4):
if i == 2:
pass
print(i)
```
A. 1 2 3
B. 1 3
C. 2 3
D. Error
✔ Answer: A
21. What is the purpose of the pass statement?
A. To terminate a loop
B. To skip the rest of the current iteration
C. To act as a placeholder where code is required but not yet written
D. To continue with the next iteration immediately
✔ Answer: C
22. What is the output of the following code?
```python
x=3
y=4
if x > y:
print("x")
elif x < y:
print("y")
else:
print("Equal")
```
A. x
B. y
C. Equal
D. Error
✔ Answer: B
23. What will be printed by this code?
```python
x = 10
while x > 5:
print(x)
x -= 2
```
A. 10 8 6
B. 10 8 6 4
C. 8 6 4
D. 10 8 6 4 2
✔ Answer: A
24. Which of the following expressions is used to check if a number is even?
A. if n % 2 = 0:
B. if n % 2 == 0:
C. if n / 2 == 0:
D. if n & 2 == 0:
✔ Answer: B
25. What will this output?
```python
for i in range(1, 6):
if i == 3:
break
print(i)
```
A. 1 2 3
B. 1 2
C. 1 2 3 4 5
D. Error
✔ Answer: B
26. Which keyword is used to end a loop prematurely?
A. pass
B. stop
C. break
D. exit
✔ Answer: C
27. How many times will the loop run?
```python
for i in range(5):
print(i)
```
A. 4
B. 5
C. 6
D. Infinite
✔ Answer: B
28. What is the result of this expression if x = 5?
```python
x > 3 and x < 10
```
A. True
B. False
C. Error
D. None
✔ Answer: A
29. What is wrong with this code?
```python
x = 10
if x > 5
print("High")
```
A. Missing indentation
B. Missing colon
C. Incorrect operator
D. Syntax error in print
✔ Answer: B
30. What is the output?
```python
x=1
while x < 4:
print(x)
x += 1
else:
print("Done")
```
A. 1 2 3 Done
B. 1 2 3
C. Done only
D. Infinite loop
✔ Answer: A
31. Which of these loops is preferred when the number of iterations is known?
A. while
B. for
C. if
D. else
✔ Answer: B
32. What is printed by this code?
```python
for i in range(5, 0, -1):
print(i)
```
A. 1 2 3 4 5
B. 5 4 3 2 1
C. 5 3 1
D. Infinite loop
✔ Answer: B
33. What is the output of this?
```python
x=0
while x < 3:
print(x)
```
A. 0 1 2
B. 0
C. Infinite loop
D. Error
✔ Answer: C
34. Which logical operator will return True if at least one condition is True?
A. and
B. or
C. not
D. xor
✔ Answer: B
35. What happens if a loop has no terminating condition?
A. Syntax error
B. Infinite loop
C. Loop executes once
D. Program crashes
✔ Answer: B
36. Which is the correct way to write an else block?
A. else:
B. else()
C. else\[]
D. else;
✔ Answer: A
37. What is the output of this code?
```python
x = 10
if x < 5:
print("Small")
else:
print("Large")
```
A. Small
B. Large
C. Error
D. None
✔ Answer: B
38. Which statement is correct about nested loops?
A. Inner loop cannot access outer loop variables
B. Outer loop runs faster than inner loop
C. Inner loop completes all its iterations before the next iteration of the outer loop
D. Nested loops are not allowed in Python
✔ Answer: C
39. What is the output of this code?
```python
for i in range(3):
if i == 1:
continue
print(i)
```
A. 0 1 2
B. 0 2
C. 1 2
D. 2
✔ Answer: B
40. What will be the output of this code?
```python
x=3
y=5
if x > y:
print("x")
else:
print("y")
```
A. x
B. y
C. None
D. Error
✔ Answer: B
41. Which of these is true about the else clause in a loop?
A. It runs only if the loop is terminated with break
B. It runs only if the loop completes without hitting break
C. It runs after every iteration of the loop
D. It is used to handle exceptions
✔ Answer: B
42. What is wrong with this loop?
```python
for i in range(5):
print(i)
```
A. Incorrect loop declaration
B. Missing indentation
C. Syntax error in print statement
D. No error
✔ Answer: B
43. What will be printed?
```python
x=4
while x > 0:
print(x)
x -= 1
```
A. 4 3 2 1 0
B. 4 3 2 1
C. 4 3 2 1 0 -1
D. Infinite loop
✔ Answer: B
44. What is the output of the following nested loop?
```python
for i in range(2):
for j in range(2):
print(i + j)
```
A. 0 1 2 3
B. 0 1 1 2
C. 0 1 0 1
D. 1 2 3 4
✔ Answer: B
45. Identify the error in this code snippet.
```python
x=5
if x = 5:
print("Equal")
```
A. Incorrect comparison operator
B. Syntax error in print statement
C. Indentation error
D. No error
✔ Answer: A
46. What will this output?
```python
for i in range(3):
if i == 1:
break
print(i)
else:
print("Done")
```
A. 0 1 Done
B. 0 Done
C. 0
D. 0 1
✔ Answer: C
47. Which is a correct way to write a condition to check if a variable x is not equal to 5?
A. if x != 5:
B. if x =! 5:
C. if not x = 5:
D. if x = 5
✔ Answer: A
48. What will be the output?
```python
x = 10
while x > 0:
print(x)
x -= 3
```
A. 10 7 4 1
B. 10 9 8 7
C. 10 7 4
D. 10 7 4 1 -2
✔ Answer: A
49. Which statement best describes the use of the continue statement?
A. Ends the loop immediately
B. Skips the current iteration and proceeds with the next one
C. Starts the loop over from the beginning
D. Stops the program execution
✔ Answer: B
50. What will this code print?
```python
x=2
if x > 3:
print("High")
elif x > 1:
print("Medium")
else:
print("Low")
```
A. High
B. Medium
C. Low
D. None
✔ Answer: B
51. How many times will this loop execute?
```python
count = 0
while count < 5:
print(count)
count += 1
```
A. 4
B. 5
C. 6
D. Infinite
✔ Answer: B
52. Which operator is used for logical negation?
A. and
B. or
C. not
D. xor
✔ Answer: C
53. What is the output of this loop?
```python
for i in range(3):
for j in range(2):
print(i, j)
```
A. 0 0 1 1 2 2
B. 0 0 0 1 1 0 1 1
C. 0 0 0 1 1 0 1 1
D. 0 0 1 0 2 0
✔ Answer: B
54. Identify the error in this code:
```python
if 5 > 3
print("True")
```
A. Missing indentation
B. Missing colon
C. Incorrect comparison
D. Syntax error in print
✔ Answer: B
55. What is the output of this code?
```python
for i in range(1, 4):
print(i)
else:
print("Loop completed")
```
A. 1 2 3 Loop completed
B. 1 2 3
C. Loop completed only
D. Error
✔ Answer: A
56. Which loop type is most appropriate when you don't know how many times the loop should run?
A. for
B. while
C. if
D. else
✔ Answer: B
57. What will this code print?
```python
x=0
while x < 3:
if x == 1:
x += 1
continue
print(x)
x += 1
```
A. 0 1 2
B. 0 2
C. 1 2
D. 0 1 2 3
✔ Answer: B
58. What is wrong with this code?
```python
for i in range(3):
print(i)
```
A. Incorrect range function
B. Missing colon
C. Missing indentation
D. Syntax error in print
✔ Answer: C
59. What will be printed by the following code?
```python
x = 10
if x < 5:
print("Low")
elif x < 15:
print("Medium")
else:
print("High")
```
A. Low
B. Medium
C. High
D. None
✔ Answer: B
60. Which condition is correctly written to check if x is not equal to 5?
A. if x =! 5:
B. if x != 5:
C. if not x == 5:
D. Both B and C
✔ Answer: D
61. Which loop construct is best when the number of iterations is fixed?
A. while
B. for
C. if
D. else
✔ Answer: B
62. What will this code output?
```python
for i in range(2):
print("Loop", i)
else:
print("Completed")
```
A. Loop 0 Loop 1 Completed
B. Loop 0 Loop 1
C. Completed only
D. Syntax error
✔ Answer: A
63. What is wrong with this code?
```python
x=5
if x = 5:
print("Equal")
```
A. Incorrect comparison operator
B. Missing colon
C. Indentation error
D. No error
✔ Answer: A
64. What will be printed?
```python
x=3
while x > 0:
print(x)
x -= 1
```
A. 3 2 1
B. 3 2 1 0
C. 2 1 0
D. Infinite loop
✔ Answer: A
65. What will this output?
```python
for i in range(5):
if i % 2 == 0:
continue
print(i)
```
A. 0 2 4
B. 1 3
C. 1 3 5
D. 2 4
✔ Answer: B
66. Which of the following is true about the break statement?
A. It skips the current iteration
B. It exits the loop completely
C. It acts as a placeholder
D. It checks the condition
✔ Answer: B
67. What is the output?
```python
x=2
if x == 2:
print("Yes")
else:
print("No")
```
A. Yes
B. No
C. Error
D. None
✔ Answer: A
68. What will be printed?
```python
for i in range(3):
for j in range(2):
print(i, j)
```
A. 0 0 1 1 2 2
B. 0 0 0 1 1 0 1 1
C. 0 0 1 0 2 0
D. Error
✔ Answer: B
69. What is the correct way to write a loop that counts down from 5 to 1?
A. for i in range(5, 0, -1):
B. for i in range(1, 5):
C. while i < 5:
D. for i in range(5, 1):
✔ Answer: A
70. Which logical operator is used to check if both conditions are True?
A. and
B. or
C. not
D. xor
✔ Answer: A
71. What will be the output of this code?
```python
x=3
y=5
if x > y:
print("x")
else:
print("y")
```
A. x
B. y
C. None
D. Error
✔ Answer: B
72. How many times will this loop execute?
```python
for i in range(4):
print(i)
```
A. 3
B. 4
C. 5
D. Infinite
✔ Answer: B
73. What is the purpose of the else block in loops?
A. To run if the loop ends naturally without break
B. To handle exceptions
C. To exit the loop immediately
D. To run only if a condition is False
✔ Answer: A
74. Identify the mistake in the following code:
```python
x = 10
if x > 5
print("High")
```
A. Missing indentation
B. Missing colon
C. Incorrect operator
D. Syntax error in print
✔ Answer: B
75. What will this code print?
```python
x=0
while x < 3:
print(x)
x += 1
```
A. 0 1 2
B. 1 2 3
C. 0 1 2 3
D. Infinite loop
✔ Answer: A
76. What is the output?
```python
for i in range(3):
if i == 1:
break
print(i)
else:
print("Done")
```
A. 0 1 Done
B. 0 Done
C. 0
D. 0 1
✔ Answer: C
77. What will be the output of this code?
```python
x=5
y = 10
if x > y:
print("x is greater")
elif x < y:
print("y is greater")
else:
print("x and y are equal")
```
A. x is greater
B. y is greater
C. x and y are equal
D. Syntax error
✔ Answer: B
78. Identify the error in this loop:
```python
for i in range(5)
print(i)
```
A. Missing colon
B. Incorrect indentation
C. Syntax error in print
D. Incorrect range function
✔ Answer: A
79. What will be printed by this code?
```python
for i in range(3):
for j in range(3):
if i == j:
break
print(i, j)
```
A. 0 1 0 2 1 2
B. 0 1 0 2 1 2 2 2
C. 0 1 1 2 2 2
D. 0 1 only
✔ Answer: A
80. What is the output of this code?
```python
x=0
while x < 3:
print("Looping")
x += 1
```
A. Looping
B. Looping printed once
C. Looping printed three times
D. Infinite loop
✔ Answer: C
81. Which of the following will create an infinite loop?
A. while True:
B. for i in range(5):
C. while x > 10:
D. None of the above
✔ Answer: A
82. What will be printed?
```python
x=3
if x > 5:
print("More")
elif x > 2:
print("Some")
else:
print("Less")
```
A. More
B. Some
C. Less
D. Syntax error
✔ Answer: B
83. Identify the logical error in this code:
```python
x=4
if x > 3:
print("Greater")
```
A. Missing colon
B. Indentation error
C. Incorrect comparison operator
D. None of the above
✔ Answer: B
84. What is the purpose of the pass statement in Python loops?
A. Skip current iteration
B. Act as a placeholder
C. Exit the loop
D. Restart the loop
✔ Answer: B
85. What will be the output of this code?
```python
for i in range(4):
if i == 2:
break
print(i)
else:
print("Finished")
```
A. 0 1 Finished
B. 0 1
C. 0 1 2 Finished
D. Syntax error
✔ Answer: B
86. Which of the following is a correct way to check if x is an even number?
A. if x / 2 == 0:
B. if x % 2 == 0:
C. if x // 2 == 0:
D. if x & 2 == 0:
✔ Answer: B
87. What is the output?
```python
x=1
while x <= 3:
print(x)
x += 1
```
A. 1 2 3
B. 1 2 3 4
C. 0 1 2
D. Infinite loop
✔ Answer: A
88. Which statement about break and continue is correct?
A. break skips to the next iteration, continue exits the loop
B. break exits the loop, continue skips the current iteration
C. Both are used to terminate loops
D. Neither is valid in Python
✔ Answer: B
89. What will this code print?
```python
for i in range(3):
if i == 1:
continue
print(i)
```
A. 0 1 2
B. 0 2
C. 1 2
D. Error
✔ Answer: B
90. What is the output of this code?
```python
x=5
if x < 3:
print("Small")
elif x < 10:
print("Medium")
else:
print("Large")
```
A. Small
B. Medium
C. Large
D. Error
✔ Answer: B
91. Identify the error:
```python
x = 10
if x = 10:
print("Correct")
```
A. Incorrect indentation
B. Syntax error: wrong assignment operator
C. Incorrect print statement
D. None of the above
✔ Answer: B
92. What will be the output?
```python
for i in range(3):
for j in range(2):
print(i, j)
```
A. 0 0 1 1 2 2
B. 0 0 0 1 1 0 1 1
C. 0 0 1 0 2 0
D. Syntax error
✔ Answer: B
93. What will this code output?
```python
x=0
while x < 3:
x += 1
print(x)
```
A. 1 2 3
B. 0 1 2
C. 1 2 3 4
D. Infinite loop
✔ Answer: A
94. Which of the following is valid to write a loop?
A. for i in range(5)
B. for i in range(5):
C. for i range(5):
D. for i = range(5):
✔ Answer: B
95. What will be printed?
```python
x=3
if x > 4:
print("More")
elif x > 2:
print("Enough")
else:
print("Less")
```
A. More
B. Enough
C. Less
D. Syntax error
✔ Answer: B
96. Which statement is used to terminate a loop?
A. pass
B. continue
C. break
D. exit
✔ Answer: C
97. What will this code output?
```python
for i in range(2):
print("Outer loop")
for j in range(2):
print("Inner loop")
```
A. Outer loop Outer loop Inner loop Inner loop
B. Outer loop Inner loop Outer loop Inner loop
C. Outer loop Inner loop Inner loop Outer loop
D. Syntax error
✔ Answer: A
98. Identify the error:
```python
x=5
if x > 3
print("Yes")
```
A. Missing colon after if
B. Incorrect indentation
C. Syntax error in print
D. None of the above
✔ Answer: A
99. What is printed by this code?
```python
x=5
while x > 0:
print(x)
x -= 1
```
A. 5 4 3 2 1 0
B. 5 4 3 2 1
C. 4 3 2 1 0
D. Infinite loop
✔ Answer: B
100. Which of these is correct to run a block only when the loop completes without break?
A. else block
B. finally block
C. except block
D. continue block
✔ Answer: A