MCQs on Control Structures | Python programming

1. Conditional Statements (if, else, elif)

  1. Which of the following is the correct syntax for an if-else statement in Python?
    • a) if condition: else
    • b) if condition: elif: else
    • c) if condition else:
    • d) if condition: else condition
  2. What will be the output of the following code?
    x = 10 if x > 5: print("Greater") else: print("Smaller")
    • a) Greater
    • b) Smaller
    • c) Error
    • d) None
  3. How many elif statements can be used in a Python conditional structure?
    • a) One
    • b) Unlimited
    • c) Two
    • d) Zero
  4. What is the output of the following code?
    x = 5 if x > 10: print("Greater") elif x == 5: print("Equal") else: print("Smaller")
    • a) Greater
    • b) Equal
    • c) Smaller
    • d) Error
  5. What is the purpose of the else block in a conditional statement?
    • a) To execute code if the condition is false
    • b) To execute code when a condition is true
    • c) To end the program
    • d) To handle exceptions

2. Loops: for, while

  1. Which loop will execute a block of code a fixed number of times?
    • a) for loop
    • b) while loop
    • c) do-while loop
    • d) loop()
  2. What is the correct syntax for a for loop in Python?
    • a) for x in range():
    • b) for x in range(1, 10):
    • c) for x;
    • d) for (x = 0; x < 10; x++)
  3. What will the following code print?
    for i in range(3): print(i)
    • a) 0 1 2
    • b) 1 2 3
    • c) 0 1 2 3
    • d) Error
  4. What is the result of the following code?pythonCopy codex = 0 while x < 3: print(x) x += 1
    • a) 0 1 2
    • b) 0 1 2 3
    • c) 1 2 3
    • d) Error
  5. Which of the following will create an infinite loop in Python?
    • a) while True:
    • b) for x in range(1, 10):
    • c) while x < 10:
    • d) for x in range(10):

3. Break, Continue, and Pass Statements

  1. What does the break statement do in a loop?
    • a) Terminates the loop
    • b) Skips to the next iteration
    • c) Exits the current function
    • d) Pauses the loop
  2. What is the purpose of the continue statement in a loop?
    • a) Skips to the next iteration of the loop
    • b) Exits the loop immediately
    • c) Pauses the loop
    • d) Ends the loop
  3. What does the pass statement do in Python?
    • a) Skips the rest of the loop and proceeds to the next iteration
    • b) Terminates the loop
    • c) Does nothing, acts as a placeholder
    • d) Exits the program
  4. What will the following code print?
    for i in range(3): if i == 1: break print(i)
    • a) 0
    • b) 0 1
    • c) 0 1 2
    • d) Error
  5. What will the following code print?pythonCopy codefor i in range(3): if i == 1: continue print(i)
    • a) 0 1 2
    • b) 0 2
    • c) 1 2
    • d) Error

4. Nested Loops and Conditions

  1. Which of the following represents a nested for loop in Python?
    • a)
    pythonCopy codefor i in range(3): for j in range(3): print(i, j)
    • b)
    pythonCopy codefor i in range(3): if i == 1: for j in range(3): print(i, j)
    • c)
    pythonCopy codefor i in range(3): while i < 3: print(i)
    • d) None of the above
  2. How many times will the inner print statement execute in the following code?
    for i in range(2): for j in range(3): print(i, j)
    • a) 2
    • b) 3
    • c) 6
    • d) 5
  3. What will the following code output?
    for i in range(2): for j in range(2): if i == 1 and j == 1: break print(i, j)
    • a) 0 0 0 1 1 0
    • b) 0 0 0 1
    • c) 0 0 0 1 1 1
    • d) 0 0 0 1
  4. What is the result of the following code?
    for i in range(2): for j in range(2): if i == j: pass else: print(i, j)
    • a) 0 1 1 0
    • b) 1 0
    • c) 0 1 1 1
    • d) 0 1
  5. What is the outcome of the following code?
    for i in range(3): for j in range(3): if j == 1: break print(i, j)
    • a) 0 0 1 0
    • b) 0 0 1 1
    • c) 0 0
    • d) 1 0

5. List Comprehensions

  1. Which of the following is the correct syntax for a list comprehension?
    • a) [x for x in range(5)]
    • b) for x in range(5): [x]
    • c) x = [for x in range(5)]
    • d) list comprehension: [x for x in range(5)]
  2. What does the following list comprehension return?
    [x * 2 for x in range(3)]
    • a) [0, 1, 2]
    • b) [0, 2, 4]
    • c) [0, 2, 4, 6]
    • d) [2, 4, 6]
  3. What will this list comprehension return?
    [x for x in range(5) if x % 2 == 0]
    • a) [1, 3]
    • b) [0, 2, 4]
    • c) [1, 2, 3, 4]
    • d) [0, 1, 2]
  4. What is the output of the following code?
    [x ** 2 for x in range(3)]
    • a) [1, 4, 9]
    • b) [1, 2, 3]
    • c) [0, 1, 2]
    • d) [0, 1, 4]
  5. Which of the following will create a list of even numbers between 1 and 10 (inclusive)?
    • a) [x for x in range(1, 11) if x % 2 == 0]
    • b) [x for x in range(1, 10) if x % 2 != 0]
    • c) [x for x in range(0, 10) if x % 2 != 0]
    • d) [x for x in range(10) if x % 2 == 0]
  6. How do you create a list of squares of numbers from 0 to 4 using list comprehension?
    • a) [x * x for x in range(5)]
    • b) [x ** 2 for x in range(4)]
    • c) [x^2 for x in range(5)]
    • d) [x for x in range(5)]
  7. What will be the output of the following list comprehension?
    [x + 1 for x in range(5) if x % 2 == 0]
    • a) [2, 4]
    • b) [1, 3, 5]
    • c) [1, 3]
    • d) [0, 2, 4]
  8. Which of the following is the correct syntax for a nested list comprehension?
    • a) [x for x in range(3) for y in range(3)]
    • b) [x, y for x in range(3) for y in range(3)]
    • c) [[x, y] for x in range(3) for y in range(3)]
    • d) [[x] for x in range(3) for y in range(3)]
  9. What is the output of this code?pythonCopy code[[x * y for x in range(2)] for y in range(2)]
    • a) [[0, 0], [0, 1]]
    • b) [[0, 0], [0, 2]]
    • c) [[0, 1], [0, 2]]
    • d) [[0, 2], [0, 1]]
  10. What does this list comprehension do?pythonCopy code[x for x in range(5) if x % 2 == 1]
    • a) Generates a list of odd numbers between 1 and 5
    • b) Generates a list of even numbers between 1 and 5
    • c) Generates a list of prime numbers between 1 and 5
    • d) Generates a list of all numbers between 1 and 5

Answers (Tabular Form)

Question No.Answer
1b
2a
3b
4b
5a
6a
7b
8a
9a
10a
11a
12a
13c
14a
15b
16a
17c
18b
19d
20c
21a
22b
23b
24a
25a
26a
27a
28c
29b
30a

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top