# 30+ MCQs on Python Control Flow(If Statements and Loops)

Ayushi Trivedi 19 Mar, 2024

Welcome to our Python Control Flow quiz! Control flow structures like `if` statements, `while` loops, and `for` loops are essential for directing the flow of execution in Python programs. This quiz will test your understanding of how to use these structures effectively to make decisions, iterate over sequences, and control program flow. Get ready to sharpen your skills and deepen your understanding of Python control Python Interview Questions.

## 30+ Python Interview Questions on Python Control Flow(If Statements and Loops)

#### Q1. What is the primary purpose of the if statement in Python?

a) To execute a block of code based on a condition

b) To perform mathematical operations

c) To repeat a block of code

d) To define a function

Explanation: The if statement is used to execute a block of code if a specified condition is true.

#### Q2. What will be the output of the following code snippet?

``````x = 10
if x > 5:
print("x is greater than 5")
``````

a) x is greater than 5

b) x is less than 5

c) x is equal to 5

d) No output

Explanation: Since the condition `x > 5` is true, the code block within the `if` statement is executed, resulting in “x is greater than 5” being printed.

#### Q3. Which keyword is used to execute a block of code if the condition in the if statement is false?

a) else

b) elif

c) while

d) for

Explanation: The else keyword is used to execute a block of code if the condition in the if statement is not true.

#### Q4. What is the purpose of the elif statement in Python?

a) To execute a block of code if the previous conditions are false

b) To define a loop

c) To perform arithmetic operations

d) To exit from a loop

Explanation: The elif statement is used to check additional conditions if the previous conditions in the if statement are false.

#### Q5. What will be the output of the following code snippet?

``````x = 5
if x < 3:
print("x is less than 3")
elif x == 3:
print("x is equal to 3")
else:
print("x is greater than 3")
``````

a) x is less than 3 b) x is equal to 3 c) x is greater than 3 d) No output

Explanation: Since none of the previous conditions were met, the `else` block is executed, resulting in “x is greater than 3” being printed.

#### Q6. How can you execute multiple statements under a single if block in Python?

a) Separate statements with a semicolon

b) Indent the statements to the same level

c) Use the and keyword between statements

d) Use the elif keyword

Explanation: In Python, multiple statements under a single if block are executed by indenting them to the same level.

#### Q7. What will the following code snippet print?

``````x = 10
if x < 5:
print("x is less than 5")
elif x > 15:
print("x is greater than 15")
else:
print("x is between 5 and 15")
``````

a) x is less than 5 b) x is greater than 15 c) x is between 5 and 15 d) No output

Explanation: Since none of the previous conditions were met, the `else` block is executed, resulting in “x is between 5 and 15” being printed.

#### Q8. What is the purpose of the while loop in Python?

a) To execute a block of code repeatedly until a condition is false

b) To execute a block of code a fixed number of times

c) To define a function

d) To iterate over items in a sequence

Explanation: The while loop is used to execute a block of code repeatedly until a specified condition is false.

#### Q9. What is the syntax for a while loop in Python?

a) while condition:

b) while condition():

c) while (condition):

d) while loop condition:

Explanation: In Python, the while loop syntax requires the condition to be followed by a colon (:).

#### Q10. How can you exit a loop prematurely in Python?

a) Using the break statement
b) Using the continue statement
c) Using the pass statement
d) Using the exit function

Explanation: The break statement is used to exit a loop prematurely, regardless of the loop condition, and move to the next statement outside the loop.

#### Q11. What is the purpose of the for loop in Python?

a) To execute a block of code repeatedly until a condition is false
b) To iterate over items in a sequence
c) To execute a block of code a fixed number of times
d) To define a function

Explanation: The for loop is used to iterate over items in a sequence such as lists, tuples, dictionaries, or strings.

#### Q12. What is the syntax for a for loop in Python?

a) for item in sequence:
b) for item in range(n):
c) for index in range(len(sequence)):
d) All of the above

Explanation: Python offers multiple ways to iterate using a for loop, including iterating directly over items in a sequence or using the range() function.

#### Q13. What will the following code snippet print?

``````fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
``````

a) apple banana cherry
b) [“apple”, “banana”, “cherry”]
c) 0 1 2
d) No output

Explanation: The `for` loop iterates over each element in the `fruits` list and prints each element individually.

#### Q14. How can you skip the current iteration of a loop and continue with the next iteration?

a) Using the skip statement
b) Using the pass statement
c) Using the break statement
d) Using the continue statement

Explanation: The continue statement skips the current iteration of a loop and proceeds with the next iteration.

#### Q15. What is the purpose of the range() function in Python?

a) To generate a sequence of numbers
b) To iterate over items in a sequence
c) To define a function
d) To execute a block of code repeatedly until a condition is false

Explanation: The range() function generates a sequence of numbers usable for iteration, as indices, or any purpose requiring a sequence of numbers.

#### Q16. What will the following code snippet print?

``````for i in range(3):
print(i)
``````

a) 0 1 2
b) 1 2 3
c) 2 1 0
d) 3 2 1 0

Explanation: The `range(3)` function generates numbers from 0 to 2 (inclusive), so the loop will print each number in that range.

#### Q17. What will be the output of the following code?

``````for i in range(1, 6):
if i == 3:
continue
print(i)
``````

a) 1 2
b) 1 2 3
c) 1 2 4 5
d) 1 2 4

Explanation: The `continue` statement skips the rest of the loop and moves to the next iteration. So when `i` equals 3, it skips printing that value.

#### Q18. What will be the output of the following code?

``````for i in range(3):
for j in range(3):
print(i + j, end=' ')
print()
``````

a) 0 1 2
1 2 3
2 3 4

b) 0 1 2
1 2 3
2 3 4
3 4 5

c) 0 1 2
1 2 3
2 3 4
4 5 6

d) 0 1 2
2 3 4
4 5 6

Explanation: The outer loop iterates three times, and for each iteration of the outer loop, the inner loop also iterates three times. The values of `i` and `j` are added together and printed. Each iteration of the outer loop starts a new line.

#### Q19. What will be printed by the following code?

``````num = 5
while num > 0:
print(num)
num -= 1
if num == 3:
break
else:
print("Done")
``````

a) 5 4 3 2 1
b) 5 4
c) Done
d) 5 4 3

Explanation: The `while` loop iterates as long as `num` is greater than 0. Inside the loop, `num` is decremented by 1 in each iteration. When `num` becomes 3, the `break` statement is encountered, and the loop terminates.

#### Q20. What is the output of the following code?

``````x = 10
if x > 5:
print("A")
elif x > 7:
print("B")
else:
print("C")
``````

a) A
b) B
c) C
d) A and B

Explanation: The condition `x > 5` evaluates to True since `x` is 10. Therefore, the code inside the `if` block executes, printing “A”.

#### Q21. What will be the output of the following code?

``````x = 5
while x > 0:
print(x, end=" ")
x -= 2
if x == 1:
break
else:
print("Done")
``````

a) 5 3 1
b) 5 3
c) 5 3 Done
d) 5 3 1 Done

Explanation: The `while` loop iterates until `x` becomes 1. Inside the loop, `x` is decremented by 2 in each iteration. When `x` becomes 1, the loop breaks, and the `else` block is not executed.

#### Q22. What is the output of the following code?

``````for i in range(5):
if i == 2:
continue
print(i, end=" ")
``````

a) 0 1 3 4
b) 0 1 2 3 4
c) 0 1 3 4 5
d) 0 1 2 3 4 5

Explanation: The `continue` statement skips the current iteration when `i` equals 2. Therefore, 2 is not printed.

#### Q23. What is the output of the following code?

``````num = 0
while num < 5:
print(num)
num += 1
else:
print("Loop completed.")
``````

a) 0 1 2 3 4
b) 0 1 2 3 4 Loop completed.
c) Loop completed.
d) This code will result in an error.

Explanation: The `while` loop iterates until `num` is less than 5, printing the value of `num` in each iteration. Once `num` becomes 5, the loop terminates, and the `else` block is executed.

#### Q24. What will be the output of the following code?

``````x = 10
if x > 5:
print("Greater than 5")
if x > 8:
print("Greater than 8")
if x > 12:
print("Greater than 12")
else:
print("Equal or less than 12")
``````

a) Greater than 5
Greater than 8
Equal or less than 12

b) Greater than 5
Greater than 8
Greater than 12
Equal or less than 12

c) Greater than 5
Greater than 8

d) Equal or less than 12

Explanation: Each `if` statement is independent of the others, so all conditions that are true will execute their corresponding `print` statements.

#### Q25. What will be the output of the following code?

``````for i in range(3):
for j in range(3):
print(i * j, end=' ')
print()
``````

a) 0 0 0
0 1 2
0 2 4

b) 0 1 2
0 2 4
0 3 6

c) 0 0 0
0 1 2
0 2 4
0 3 6

d) 0 0 0
0 1 2
0 1 2

Explanation: The code uses nested loops to iterate over each combination of `i` and `j`, printing their product. The `print()` function with no arguments prints a newline, separating each row.

#### Q26. What will be the output of the following code?

``````num = 10
while num > 0:
print(num)
num -= 3
else:
print("Loop completed.")
``````

a) 10 7 4 1 Loop completed.
b) 10 7 4 1
c) Loop completed.
d) This code will result in an error.

Explanation: The `while` loop decrements `num` by 3 in each iteration until `num` becomes 0. Once `num` becomes 0, the loop terminates, and the `else` block is executed.

#### Q27. What will the following code snippet print?

``````for i in range(3):
pass
print(i)
``````

a) 0 1 2
b) 1 2 3
c) 2 1 0
d) 3 2 1 0

Explanation: The loop iterates over the numbers from 0 to 2, but the `pass` statement inside the loop does nothing, so the value of `i` remains 2 when printed outside the loop.

#### Q28. What is the purpose of the `pass` statement in Python?

a) To exit from a loop prematurely
b) To skip the current iteration of a loop
c) To execute a block of code if a condition is false
d) To do nothing and act as a placeholder

Explanation: The `pass` statement does nothing and acts as a placeholder where syntactically a statement is required but no action is desired or needed.

#### Q29. What is the purpose of the else block in a loop in Python?

a) To execute if the loop encounters an error
b) To execute if the loop completes without encountering a break statement
c) To execute if the loop encounters a continue statement
d) To execute if the loop encounters a pass statement

Explanation: The else block in a loop executes when the loop completes normally, meaning it doesn’t encounter a break statement.

#### Q30. What is the output of the following code?

``````x = 10
if x > 5:
print("Hello")
elif x > 8:
print("Hi")
else:
print("Hey")
``````

a) Hey
b) Hi
c) Hello
d) Hello, Hi

Explanation: The condition `x > 5` is true because `x` is 10, so the corresponding code block is executed, printing “Hello”. Even though `x > 8` is also true, the `elif` block is skipped because the `if` condition was already met.

#### Q31. What is the output of the following code?

``````for i in range(1, 6):
if i % 2 == 0:
print(i)
continue
print("*")
``````

a) \n2\n\n4\n*
b) \n2\n\n4\n*\n
c) \n2\n\n*\n4\n
d) \n\n2\n*\n4\n

Explanation: In this code, for each number `i` from 1 to 5, if `i` is even, it’s printed, and the loop continues to the next iteration using `continue`. If `i` is odd, “*” is printed instead.

#### Q32. What will be the output of the following code?

``````x = 5
while x > 0:
print(x)
x -= 1
else:
print("Done")
``````

a) 5\n4\n3\n2\n1\nDone
b) Done\n5\n4\n3\n2\n1
c) 5\n4\n3\n2\n1
d) Done

Explanation: The `while` loop prints the values of `x` from 5 to 1, then after `x` becomes 0, the `else` block is executed, printing “Done”.

#### Q33. What will be the output of the following code?

``````for i in range(1, 6):
if i == 3:
break
print(i)
else:
print("Loop completed.")
``````

a) 1\n2
b) 1\n2\n3\n4\n5\nLoop completed.
c) 1\n2\n3\n4\n5
d) 1\n2\n3

Explanation: The loop breaks when `i` equals 3, so the `else` block is not executed.
Congratulations on completing the Python Control Flow quiz! Control flow structures are fundamental to writing efficient and effective Python code. By mastering `if` statements, `while` loops, `for` loops, and loop control statements, you’re equipped to create programs that can make decisions, repeat tasks, and handle various scenarios with ease. Keep practicing and experimenting with different control flow scenarios to become a proficient Python programmer. Well done, and happy coding!