Python Advanced Quiz
Mutable defaults, closures, the data model, generators and the GIL — the behaviour that separates people who have used Python from people who understand it.
Start the quizSample questions from this quiz
A preview of the style and depth. Try each one, then reveal the answer — or skip straight to the timed quiz.
1. What does this print?
def add(item, items=[]):
items.append(item)
return items
print(add(1))
print(add(2))- [1] then [1, 2]
- [1] then [2]
- [1] then [1]
- TypeError
Show answer
Answer: [1] then [1, 2]. Default arguments are evaluated once, when the function is defined — not on each call. The same list object is reused and accumulates across calls. Use `items=None` and create the list inside the function. This is the single most asked Python gotcha.
2. What does this print?
fns = [lambda: i for i in range(3)]
print([f() for f in fns])- [0, 0, 0]
- [0, 1, 2]
- [2, 2, 2]
- NameError
Show answer
Answer: [2, 2, 2]. The lambdas close over the variable `i`, not its value at creation time, and by the time they are called the loop has finished with `i == 2`. Bind it eagerly with a default argument: `lambda i=i: i`. The same trap exists in JavaScript with `var`.
3. What does this print?
a = [1, 2, 3]
b = a
c = a[:]
a.append(4)
print(len(b), len(c))- 4 4
- 4 3
- 3 3
- 3 4
Show answer
Answer: 4 3. `b = a` binds another name to the same list, so it sees the append. `a[:]` creates a shallow copy, which does not. Note the copy is shallow: nested objects are still shared, which is what `copy.deepcopy` is for.
Frequently asked questions
How many questions are in this Python quiz?+
30 questions, with a 45-minute time limit. Every question includes a written explanation of the correct answer.
Is this Python quiz free?+
Yes. Every quiz on CodexQuizz is free and needs no account. You only enter a name if you choose to post your score to the leaderboard.
What level is the advanced quiz aimed at?+
Mutable defaults, closures, the data model, generators and the GIL — the behaviour that separates people who have used Python from people who understand it.
Can I use this to prepare for a Python interview?+
Yes. The questions cover the topics that come up in Python technical screens, and the explanations are written so that a wrong answer still teaches you the underlying concept.
Ready to test your Python?
30 questions, 45 minutes. Free, no sign-up.
Start now