10 Python Output Questions for Interview Preparation
Ten classic Python output patterns that appear on technical phone screens, with tracing tips.
Ameer Abdullah
Data Science Graduate · AI/ML & Data Science
Phone screens often allocate fifteen minutes to a Python snippet. You must read, trace, and explain aloud. Below are ten high-frequency patterns; practice each with a trace table before checking answers. Tracking reference counts and standard scoping overrides is the quickest way to gain technical confidence ahead of a screening.
Pattern: Mutable default arguments
def f(a, lst=[]):
lst.append(a)
return lst
print(f(1), f(2))Output: [1] [1, 2]. The default list is shared across calls because function definitions evaluate once at import runtime - a favorite assessment question for companies trying to filter foundational runtime understanding.
Pattern: Late-binding closure inside loops
Late-binding closures trip candidates who expect each lambda to capture its own index. Trace what the loop index equals when the function actually runs, not when it is created. Python closure frames preserve a reference to the outer variable context rather than a snapshot value copy, modifying output paths significantly.
Pattern: Generator Exhaustion
def gen():
yield 1
yield 2
g = gen()
print(next(g))
print(next(g))
print(next(g)) # StopIterationOutput: 1, then 2, then StopIteration is raised (program crashes unless caught). Generators maintain internal state between calls. Each call to next() resumes execution from the last yield statement. When the function body ends without a yield, Python raises StopIteration automatically. This is a favorite mid-level interview question.
Pattern: Class Variable vs Instance Variable
class Counter:
count = 0
def __init__(self):
Counter.count += 1
a = Counter()
b = Counter()
print(Counter.count)
print(a.count)Output: 2, then 2. Counter.count is a class variable shared across all instances. Each call to __init__ increments it via the class reference. When you access a.count, Python first looks at instance attributes (none defined for 'count'), then falls back to the class variable. Both a.count and Counter.count return the same shared value of 2.
Pattern: Comprehension Scope Isolation
x = 10 result = [x for x in range(3)] print(x) print(result)
Output: 10, then [0, 1, 2]. In Python 3, list comprehensions have their own scope. The 'x' inside the comprehension is local to it and does not overwrite the outer 'x = 10'. This behavior changed from Python 2 where comprehension variables leaked into the enclosing scope - a common source of bugs in legacy code and a favorite question for candidates who say they 'know Python.'
Pattern: Chained Comparison Operators
print(1 < 2 < 3) # True print(3 > 2 > 1) # True print(1 < 2 > 0) # True print(1 < 2 < 1) # False
Python supports chained comparisons which are evaluated left to right with short-circuit semantics. '1 < 2 < 3' is equivalent to '(1 < 2) and (2 < 3)'. Both must be True for the chain to return True. Interviewers use chained comparisons to probe whether candidates know Python's evaluation order or mistakenly compute '1 < 2' as True (1) and then test '1 < 3'.
Preparing Your Trace Table Vocabulary
Strong candidates narrate their trace table with precise vocabulary. Instead of 'x becomes 5', say 'the name x in the local frame is rebound to the integer object 5.' Instead of 'the list grows', say 'append() mutates the list object in place; no new object is created.' This precision signals to interviewers that you understand Python's object model, not just its surface syntax.
The 10 output questions in this guide cover the most common patterns. For comprehensive preparation, work through PyCodeIt's full dry-run library, which includes 1,225+ problems tagged by concept: closures, generators, decorators, class methods, exception handling, and more. Track which categories give you the most trouble and focus your practice there.
Quick Concept Quiz
Why does a mutable default argument like lst=[] retain values across function calls?
Ready to test your execution tracing skills?
Hop directly into the Python trace map to start coding, grading queries, and logging XP metrics to your workspace profile.