Control flow statements regulate the order in which individual code statements are evaluated and executed in a Python program. Python provides three main control flow mechanisms: sequential execution, selection statements (if, elif, else, and match case), and iteration loops (for and while). Unlike C style languages that utilize curly braces, Python relies strictly on block indentation to define conditional and loop scopes. Modern Python features like loop else clauses, ternary expressions, and structural pattern matching enhance code expressiveness and maintainability. Mastering control flow is fundamental for writing dynamic, bug-free applications and clearing technical assessments.
Python Control Flow MCQs
1 min read
Correct Answer: c) elif
Explanation:
Python uses the keyword 'elif' (short for else if) to chain multiple conditional statements together after an initial 'if'.
Correct Answer: b) B
Explanation:
This is Python's ternary conditional expression. Since x > 10 evaluates to False, the expression returns and executes the right-hand branch, printing 'B'.
Correct Answer: b) The 'else' block executes.
Explanation:
In Python, a loop's optional 'else' block executes if and only if the loop terminates normally (without hitting a break statement).
Correct Answer: b) 2
Explanation:
The loop completes and 'i' remains bound to its last iterated value in the current scope, which is 2.
Correct Answer: b) [5, 4, 3, 2]
Explanation:
The range function excludes the stop boundary (1). With a step of -1, it produces sequence elements starting at 5 down to 2.
Correct Answer: c) break
Explanation:
The 'break' statement terminates the active loop prematurely and transfers execution to the statement immediately following the loop.
Correct Answer: b) To skip the remaining code in the current iteration and move to the next iteration.
Explanation:
The 'continue' statement skips all remaining statements in the loop's body for the current iteration and advances control back to the loop header.
Correct Answer: c) It acts as a null operation placeholder where statement syntax is required.
Explanation:
'pass' is a syntactic placeholder that does nothing when executed, allowing empty blocks in structures like if statements, loops, or functions.
Correct Answer: c) match / case
Explanation:
Python 3.10 introduced structural pattern matching using the 'match' and 'case' soft keywords.
Correct Answer: b) _
Explanation:
The single underscore '_' is the wildcard pattern in structural pattern matching that matches any object if no preceding pattern matched.
Correct Answer: b) 1 2 3
Explanation:
In each iteration, x is incremented before printing. The iterations increment x to 1, then 2, then 3, printing '1 2 3 '.
Correct Answer: a) 0 1 2 End
Explanation:
The while loop prints 0, 1, and 2. When x reaches 3, x < 3 becomes False, causing the loop to finish normally and triggering the 'else' block.
Correct Answer: b) The 'else' block is skipped entirely.
Explanation:
When a loop terminates via a 'break' statement, the loop's 'else' clause is bypassed.
Correct Answer: b) False
Explanation:
An empty sequence (like an empty list []) evaluates to False in a boolean context, so the condition fails.
Correct Answer: c) [-1]
Explanation:
Any non-empty collection in Python (even one containing negative numbers or falsy items) evaluates to True.
Correct Answer: b) Out
Explanation:
The loop executes 'count += 1' and immediately hits 'break'. The 'else' block is skipped, and execution proceeds directly to 'print("Out")'.
Correct Answer: b) start=0, stop=10, step=1
Explanation:
Single-argument range(stop) defaults start to 0 and step to 1, generating numbers from 0 up to (but not including) stop.
Correct Answer: b) [0, -1, -2, -3, -4]
Explanation:
The range starts at 0 and decrements by -1 until reaching the stop limit -5 (excluded).
Correct Answer: c) []
Explanation:
By default, the step is +1. Since start (5) is greater than stop (2) with a positive step, range produces an empty sequence.
Correct Answer: b) 2 4 6
Explanation:
The loop iterates through 1, 2, and 3. Multiplying each by 2 yields 2, 4, and 6.
Correct Answer: b) 01 10
Explanation:
When i == j (at 0,0 and 1,1), 'continue' skips the print statement. Only pairs where i != j (0,1 and 1,0) are printed.
Correct Answer: b) 3 times
Explanation:
i takes values 5 (prints 1st), 3 (prints 2nd), and 1 (prints 3rd). When i becomes -1, -1 > 0 is False and the loop terminates.
Correct Answer: b) Y
Explanation:
This is a chained ternary expression. x > 15 is False, so it evaluates the second branch 'print("Y") if y > 15 else print("Z")'. Since y > 15 is True, 'Y' is printed.
Correct Answer: b) __iter__()
Explanation:
A 'for' loop calls iter() on the target sequence, which invokes the sequence's __iter__() dunder method to obtain an iterator.
Correct Answer: b) __next__()
Explanation:
The loop calls next() on the iterator object, invoking its __next__() method until StopIteration is raised.
Correct Answer: b) StopIteration
Explanation:
When __next__() runs out of elements, it raises StopIteration, which tells the 'for' loop to terminate cleanly.
Correct Answer: b) 2
Explanation:
The first call to next(it) returns 1. The second call advances the iterator and returns 2.
Correct Answer: b) case 1 | 2 | 3:
Explanation:
In structural pattern matching, the bitwise OR pipe operator '|' acts as an OR pattern separator within case expressions.
Correct Answer: b) Guard condition
Explanation:
An 'if' clause appended to a pattern in a case statement (e.g., case x if x > 0:) is called a guard condition.
Correct Answer: b) High
Explanation:
The pattern binds val (10) to x. The guard 'if x >= 5' evaluates to True (10 >= 5), so 'High' is printed.
Correct Answer: c) Indentation defines the code block associated with the condition.
Explanation:
Python relies strictly on consistent indentation levels rather than braces or delimiter keywords to define block structures.
Correct Answer: a) 1
Explanation:
a is True and b is False. 'not b' is True. Thus 'True and True' is True, printing '1'.
Correct Answer: d) The exception propagates up the call stack, potentially crashing the program.
Explanation:
Unhandled exceptions escalate up caller frames until handled by an enclosing try-except or reaching top-level execution, where Python terminates with a traceback.
Correct Answer: b) Only when NO exceptions were raised in the try block.
Explanation:
The 'else' block in exception handling executes if and only if the 'try' block completed without raising any exception.
Correct Answer: c) Always, whether an exception occurred or not.
Explanation:
The 'finally' clause is guaranteed to execute clean-up code regardless of whether the try block raised an exception, caught an exception, or returned.
Correct Answer: c) Div Fin
Explanation:
ZeroDivisionError triggers the except block (printing 'Div'). After handling, the 'finally' block executes (printing 'Fin').
Correct Answer: b) The 'finally' block's return value overwrites the 'try' return value.
Explanation:
When 'finally' executes a return statement, it discards any pending return value or active exception from the preceding try or except blocks.
Correct Answer: c) 0 times
Explanation:
The condition 10 > 10 evaluates to False on the initial check, so the loop body never executes.
Correct Answer: a) 0 2 End
Explanation:
1 is skipped due to 'continue', so 0 and 2 print. Because no 'break' occurred, the 'else' clause executes, printing 'End'.
Correct Answer: b) 0
Explanation:
At x = 0, 0 is printed. At x = 1, 'break' triggers. The loop terminates immediately, and the 'else' block is skipped.
Correct Answer: a) Equal
Explanation:
The == operator checks value equality. Since x and y have identical contents, x == y evaluates to True.
Correct Answer: b) Different
Explanation:
The 'is' operator tests object identity (memory address). Lists are distinct objects in memory, so x is y is False.
Correct Answer: b) Empty
Explanation:
The sequence [] is empty, so the loop body executes 0 times. Since no break occurred, the 'else' clause executes immediately.
Correct Answer: b) if x > 0 { print(x) }
Explanation:
Python does not use curly braces {} to define code blocks for control flow; using them results in a SyntaxError.
Correct Answer: b) 2 6
Explanation:
1st iter: x becomes 2, printed (2). 2nd iter: x becomes 4, skipped by continue. 3rd iter: x becomes 6, loop ends, printed (6).
Correct Answer: b) Resource setup and clean-up (enter/exit)
Explanation:
The 'with' statement simplifies resource management by invoking __enter__() and ensuring __exit__() executes upon exit.
Correct Answer: b) __enter__ and __exit__
Explanation:
Context manager protocol objects require __enter__() (executed before entering the with body) and __exit__() (executed on exit).
Correct Answer: b) exc_type, exc_val, exc_tb
Explanation:
When an exception occurs, Python passes the exception type, exception value, and traceback object to __exit__(exc_type, exc_val, exc_tb).
Correct Answer: a) By returning True from __exit__()
Explanation:
If the __exit__() method returns a truthy value (like True), Python suppresses the exception and continues execution normally after the with block.
Correct Answer: a) a b
Explanation:
Iterating directly over a dictionary iterates over its keys in insertion order.
Correct Answer: b) for k, v in dict.items():
Explanation:
The dict.items() method returns a view of (key, value) tuples, allowing dual variable unpacking in a loop.
Correct Answer: b) 0 x 1 y
Explanation:
enumerate() yields pairs of (index, item) starting at index 0 by default.
Correct Answer: b) start
Explanation:
The 'start' keyword argument specifies the starting integer offset for enumerate (e.g., enumerate(items, start=1)).
Correct Answer: a) 1 x 2 y
Explanation:
By default, zip() stops iterating when the shortest input iterable is exhausted.
Correct Answer: a) strict=True
Explanation:
zip(*iterables, strict=True) enforces that all zipped iterables must have identical lengths, raising ValueError otherwise.
Correct Answer: b) [2, 4]
Explanation:
This list comprehension filters elements using the condition x % 2 == 0, keeping only even numbers.
Correct Answer: b) [0, 0, 3, 4]
Explanation:
When using an inline if-else inside list comprehensions (ternary clause), it must precede the 'for' statement.
Correct Answer: b) B
Explanation:
5 < 2 is False, moving to the next expression. 5 < 10 is True, so 'B' is executed and printed.
Correct Answer: b) E
Explanation:
x > 5 is False. Next branch evaluates x == 3 which is True, so 'E' is printed.
Correct Answer: c) do { print(x) } while x < 5
Explanation:
Python does not support 'do-while' loops. Attempting to use do-while syntax results in a SyntaxError.
Correct Answer: a) using while True with conditional break at end of block
Explanation:
A 'while True' loop executing logic first and then checking a exit condition with 'if condition: break' reproduces do-while behavior.
Correct Answer: b) SyntaxError
Explanation:
In Python conditional ternary expressions (a if condition else b), both branches must be valid expressions. 'pass' is a statement, not an expression, causing a SyntaxError.
Correct Answer: b) [1, 3]
Explanation:
The comprehension checks i % 2 != 0 (odd numbers), yielding [1, 3].
Correct Answer: a) 5 values
Explanation:
The generated numbers are 10, 8, 6, 4, 2 (stopping before 0), containing 5 elements.
Correct Answer: b) all()
Explanation:
all(iterable) returns True if every element in the iterable evaluates to True (or if iterable is empty).
Correct Answer: b) any()
Explanation:
any(iterable) returns True if at least one item in the iterable is truthy.
Correct Answer: b) True and False
Explanation:
all([]) returns True by definition (vacuous truth), while any([]) returns False because no truthy element exists.
Correct Answer: b) AssertionError: Error!
Explanation:
The assert statement checks condition x == 5 (False), raising an AssertionError with the message string 'Error!'.
Correct Answer: a) Running python with -O (optimize) flag.
Explanation:
Executing Python scripts using the command line flag -O strips assert statements from compiled bytecode.
Correct Answer: b) B
Explanation:
The match expression matches literal 2 and prints 'B'.
Correct Answer: a) Even
Explanation:
4 % 2 == 0 is True, so res receives 'Even'.
Correct Answer: a) case [1, 2] as pair:
Explanation:
The 'as' keyword binds matched sub-patterns to a variable alias inside match-case statements.
Correct Answer: b) Python interpreter raises SyntaxError.
Explanation:
'break' outside a loop causes Python parser to raise 'SyntaxError: 'break' outside loop'.
Correct Answer: b) Python interpreter raises SyntaxError.
Explanation:
'continue' outside a loop triggers 'SyntaxError: 'continue' not properly in loop'.
Correct Answer: a) 1
Explanation:
In the first iteration, x is assigned 1, then 'break' terminates the loop. 'x' retains value 1 and is printed.
Correct Answer: a) 1
Explanation:
Iter 1: x=1, prints '1 '. Iter 2: x=2, condition x==1 is False, executes 'else: break', terminating loop.
Correct Answer: a) [3, 4]
Explanation:
filter() tests elements against condition x > 2, retaining only 3 and 4.
Correct Answer: b) No
Explanation:
x == 2 is True, but y == 4 is False. 'True and False' is False, so 'No' is printed.
Correct Answer: a) ValueError
Explanation:
type(e).__name__ returns the exact class name of the raised exception instance, which is 'ValueError'.
Correct Answer: a) 1 4 9
Explanation:
i takes values 1, 2, 3. i ** 2 computes 1**2=1, 2**2=4, 3**2=9.
Correct Answer: b) B
Explanation:
x > 20 is False. The second branch checks x > 5 (10 > 5), which is True, so 'B' is printed.
Correct Answer: a) In 'A and B', if A is False, B is not evaluated.
Explanation:
'and' short-circuits as soon as a falsy operand is found; 'or' short-circuits when a truthy operand is found.
Correct Answer: a) Truthy
Explanation:
[0] is a non-empty list, and non-empty lists are truthy in Python boolean evaluation.
Correct Answer: b) Nothing is printed
Explanation:
When x decrements to 2, 'break' triggers. The loop exits immediately, bypassing the loop's 'else' block.
Correct Answer: b) []
Explanation:
When start equals stop in range(start, stop), the sequence generated is empty [].
Correct Answer: a) True
Explanation:
The walrus operator := assigns the evaluation of (5 > 2), which is True, to x and returns True to the if condition.
Correct Answer: a) __enter__ runs before block execution, __exit__ runs after block execution.
Explanation:
The context manager protocol calls __enter__() prior to executing the code block, and __exit__() when exiting the block.
Related Posts
New
New
New

Python Variables & Data Types MCQs
Variables in Python act as dynamic references reserved in memory to store objects, operating without explicit data type declarations due…
August 27, 2026By MCQs Generator

Python Strings MCQs
In Python, a string (str) is an immutable sequence of Unicode characters used to represent text data. Defined using single,…
August 27, 2026By MCQs Generator

Latest Python Loops MCQs
Loops in Python provide the fundamental mechanism for executing a block of code repeatedly until a specified condition is met…
August 27, 2026By MCQs Generator
Related Categories
New












AI & Data Science MCQ
5 topics
By MCQs Generator
New
Arts & Humanities MCQ
4 topics
By MCQs Generator
New
Civil Engineering MCQ
4 topics
By MCQs Generator
New
Commerce & Business MCQ
4 topics
By MCQs Generator
New
Competitive Exams MCQ
5 topics
By MCQs Generator
New
Electrical & Electronics Engineering MCQ
3 topics
By MCQs Generator
New
General Knowledge MCQ
2 topics
By MCQs Generator
New
General Science MCQ
4 topics
By MCQs Generator
New
Law & Judiciary MCQ
3 topics
By MCQs Generator
New
Mechanical Engineering MCQ
4 topics
By MCQs Generator
New
Medical & Health Sciences MCQ
4 topics
By MCQs Generator
New
Modern Tech Fields MCQ
3 topics
By MCQs Generator