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.
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
Answer: a
Explanation: The if statement is used to execute a block of code if a specified condition is true.
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
Answer: a
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.
a) else
b) elif
c) while
d) for
Answer: a
Explanation: The else keyword is used to execute a block of code if the condition in the if statement is not true.
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
Answer: a
Explanation: The elif statement is used to check additional conditions if the previous conditions in the if statement are false.
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
Answer: c
Explanation: Since none of the previous conditions were met, the else
block is executed, resulting in “x is greater than 3” being printed.
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
Answer: b
Explanation: In Python, multiple statements under a single if block are executed by indenting them to the same level.
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
Answer: c
Explanation: Since none of the previous conditions were met, the else
block is executed, resulting in “x is between 5 and 15” being printed.
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
Answer: a
Explanation: The while loop is used to execute a block of code repeatedly until a specified condition is false.
a) while condition:
b) while condition():
c) while (condition):
d) while loop condition:
Answer: a
Explanation: In Python, the while loop syntax requires the condition to be followed by a colon (:).
a) Using the break statement
b) Using the continue statement
c) Using the pass statement
d) Using the exit function
Answer: a
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.
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
Answer: b
Explanation: The for loop is used to iterate over items in a sequence such as lists, tuples, dictionaries, or strings.
a) for item in sequence:
b) for item in range(n):
c) for index in range(len(sequence)):
d) All of the above
Answer: d
Explanation: Python offers multiple ways to iterate using a for loop, including iterating directly over items in a sequence or using the range() function.
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
Answer: a
Explanation: The for
loop iterates over each element in the fruits
list and prints each element individually.
a) Using the skip statement
b) Using the pass statement
c) Using the break statement
d) Using the continue statement
Answer: d
Explanation: The continue statement skips the current iteration of a loop and proceeds with the next iteration.
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
Answer: a
Explanation: The range() function generates a sequence of numbers usable for iteration, as indices, or any purpose requiring a sequence of numbers.
for i in range(3):
print(i)
a) 0 1 2
b) 1 2 3
c) 2 1 0
d) 3 2 1 0
Answer: a
Explanation: The range(3)
function generates numbers from 0 to 2 (inclusive), so the loop will print each number in that range.
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
Answer: c
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.
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
Answer: a
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.
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
Answer: b
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.
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
Answer: a
Explanation: The condition x > 5
evaluates to True since x
is 10. Therefore, the code inside the if
block executes, printing “A”.
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
Answer: a
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.
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
Answer: a
Explanation: The continue
statement skips the current iteration when i
equals 2. Therefore, 2 is not printed.
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.
Answer: b
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.
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
Answer: a
Explanation: Each if
statement is independent of the others, so all conditions that are true will execute their corresponding print
statements.
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
Answer: a
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.
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.
Answer: a
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.
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
Answer: c
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.
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
Answer: d
Explanation: The pass
statement does nothing and acts as a placeholder where syntactically a statement is required but no action is desired or needed.
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
Answer: b
Explanation: The else block in a loop executes when the loop completes normally, meaning it doesn’t encounter a break statement.
x = 10
if x > 5:
print("Hello")
elif x > 8:
print("Hi")
else:
print("Hey")
a) Hey
b) Hi
c) Hello
d) Hello, Hi
Answer: c
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.
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
Answer: b
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.
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
Answer: a
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”.
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
Answer: a
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!
You can also enroll in out free Python Course Today!
Read our more articles related to MCQs in Python: