Python Exam Practice Questions 2026
Python exam practice questions covering data types, loops, functions, OOP, and the standard library — with answers and explanations.
These Python exam practice questions cover the topics that appear most often on university exams, coding bootcamp assessments, and technical interviews. Each section gives you the concept, a practice question, and a worked answer — so you can check your understanding as you go.
1. Data Types & Mutability
Python's built-in types split into mutable (lists, dicts, sets) and immutable (strings, tuples, integers, frozensets). This distinction affects how variables behave when passed to functions or assigned to new names.
a = [1, 2, 3] b = a # b points at the same list object b.append(4) print(a) # [1, 2, 3, 4] — both names see the change c = (1, 2, 3) d = c # d += (4,) creates a NEW tuple; c is unchanged
Practice question: What does a = "hello"; b = a; b += " world"; print(a) print?
Answer: hello — strings are immutable, so b += " world" creates a new string object and rebinds b; a still points at the original.
2. List & Dictionary Comprehensions
Comprehensions are a concise, idiomatic way to build collections. Exams test whether you can read them and predict output correctly.
# List comprehension with filter
evens = [x for x in range(10) if x % 2 == 0]
# [0, 2, 4, 6, 8]
# Dict comprehension
squares = {x: x**2 for x in range(1, 6)}
# {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
# Nested comprehension — flatten a 2D list
matrix = [[1, 2], [3, 4], [5, 6]]
flat = [n for row in matrix for n in row]
# [1, 2, 3, 4, 5, 6]Practice question: What does [x*y for x in range(1,3) for y in range(1,3)] produce?
Answer: [1, 2, 2, 4] — x=1,y=1 → 1; x=1,y=2 → 2; x=2,y=1 → 2; x=2,y=2 → 4.
3. Functions: Default Arguments & *args / **kwargs
Default argument traps and variadic parameters are exam favourites because the behaviour surprises most beginners.
def greet(*names, greeting="Hello"):
for name in names:
print(f"{greeting}, {name}!")
greet("Alice", "Bob") # Hello, Alice! / Hello, Bob!
greet("Carol", greeting="Hi") # Hi, Carol!
def merge(**kwargs):
return kwargs
print(merge(x=1, y=2)) # {'x': 1, 'y': 2}Practice question: Which call is invalid: greet("Alice", "Bob"), greet(greeting="Hi", "Carol"), or greet()?
Answer: greet(greeting="Hi", "Carol") — a positional argument cannot follow a keyword argument. This raises a SyntaxError.
4. Object-Oriented Programming: Inheritance & super()
Exams test class hierarchies, method overriding, and how super() delegates to the parent class.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound"
class Dog(Animal):
def speak(self):
return f"{self.name} barks"
class Poodle(Dog):
def speak(self):
base = super().speak() # calls Dog.speak
return f"{base} (quietly)"
p = Poodle("Fifi")
print(p.speak()) # Fifi barks (quietly)Practice question: If Poodle did not define speak(), what would p.speak() return?
Answer: Fifi barks — Python's MRO (Method Resolution Order) looks up the class hierarchy and finds Dog.speak() next.
5. Iterators & the for Loop Protocol
Understanding how Python's for loop works under the hood is tested in advanced exams and interviews.
# for x in obj is equivalent to:
it = iter(obj) # calls obj.__iter__()
while True:
try:
x = next(it) # calls it.__next__()
except StopIteration:
break
# enumerate gives (index, value) pairs
for i, ch in enumerate("abc"):
print(i, ch) # 0 a / 1 b / 2 c
# zip stops at the shorter iterable
for a, b in zip([1,2,3], [10,20]):
print(a, b) # 1 10 / 2 20Practice question: What does list(zip([1,2,3], [4,5])) return?
Answer: [(1, 4), (2, 5)] — zip stops as soon as the shortest iterable is exhausted, so the third element of the first list is dropped.
6. Standard Library: collections, itertools, functools
Python exams increasingly test standard library knowledge, especially collections and functools.
from collections import Counter, defaultdict
from functools import reduce
words = ["cat", "dog", "cat", "fish", "dog", "cat"]
counts = Counter(words)
print(counts.most_common(2)) # [('cat', 3), ('dog', 2)]
dd = defaultdict(list)
dd["fruits"].append("apple") # no KeyError on missing key
total = reduce(lambda acc, x: acc + x, [1, 2, 3, 4], 0)
print(total) # 10Practice question: What does Counter("banana")["a"] return?
Answer: 3 — Counter on a string counts each character; 'a' appears three times in "banana".
7. Exception Handling: raise, re-raise, and custom exceptions
Exam questions go beyond basic try/except — they test chained exceptions, re-raising, and custom exception classes.
class InsufficientFundsError(ValueError):
def __init__(self, amount, balance):
super().__init__(f"Need {amount}, have {balance}")
self.amount = amount
self.balance = balance
def withdraw(balance, amount):
if amount > balance:
raise InsufficientFundsError(amount, balance)
return balance - amount
try:
withdraw(50, 100)
except InsufficientFundsError as e:
print(e) # Need 100, have 50
print(e.amount) # 100
raise # re-raises the same exceptionPractice question: What is the difference between raise (bare) and raise e?
Answer: Bare raise re-raises the current exception preserving the original traceback. raise e raises the same exception but resets the traceback to the current line, losing the original call stack.
Test yourself under exam conditions
Working through practice questions here is a good start, but you need timed pressure to know how you'll perform on the day. The Examifyr Python readiness test gives you 30 exam-style questions with an instant score and a topic-by-topic breakdown — free, no sign-up required.
Think you're ready? Prove it.
Take the free Python readiness test. Get a score from 0–100, a topic breakdown, and your exact weak areas — in under 20 minutes.
Take the free Python test →Free · No sign-up · Instant results