Home › Python interview questions

Python debugging interview questions — with answers you can practise

💼 10 real screening questionsEvery answer is practisableFree · no login

How to use this page

These are the questions that actually appear in Python screening rounds — not algorithm puzzles, but the short "what does this print?" and "why does this break?" probes that test whether you know the language or merely recognise it. For each one: try to answer before reading, then open the linked interactive problem and prove it in code. Reading answers feels like knowing; reproducing them under a blinking cursor is knowing.

Want the drill format instead? The Interview Practice learning path sequences 16 of these traps with progress tracking, and the output prediction archive has the predict-the-output bank.

Q1. What does this print: def f(x, items=[]): items.append(x); return items — called twice?

f(1) returns [1] but f(2) returns [1, 2], not [2]. Default values are evaluated once at def time, so every call without the argument shares one list. This is the single most-asked Python interview trap.

🛠️ Try it yourself in the interactive editor →

Q2. Why does my_list.sort() return None?

In-place methods return None by convention: sort(), reverse(), append() mutate and return nothing, while sorted() and reversed() return new objects. result = my_list.sort() therefore stores None — a classic wrong-answer generator in interviews.

🛠️ Try it yourself in the interactive editor →

Q3. What is the difference between b = a and b = a.copy() for lists?

b = a creates a second name for the same object — mutations through either name are visible to both. a.copy() (or a[:] or list(a)) copies one level; nested lists inside are still shared until you use copy.deepcopy().

🛠️ Try it yourself in the interactive editor →

Q4. When does the else block of a for loop run?

When the loop finishes without hitting break — including the zero-iteration case of an empty iterable. It is a 'no break' clause, not an 'empty loop' clause; interviewers love it because most candidates guess backwards.

🛠️ Try it yourself in the interactive editor →

Q5. What does [[0] * 3] * 2 build?

A list of two references to the SAME inner list. Setting grid[0][0] = 1 changes both rows. List multiplication copies references, not objects — build independent rows with a comprehension: [[0] * 3 for _ in range(2)].

🛠️ Try it yourself in the interactive editor →

Q6. If a try block returns, does finally still run?

Yes — the return value is computed and parked, finally executes, then the value leaves the function. And if finally itself returns, that value wins and even in-flight exceptions are discarded. State the exact sequencing and you stand out.

🛠️ Try it yourself in the interactive editor →

Q7. Why is {'a': 1, 'a': 2} legal, and what is its length?

Duplicate keys collapse silently — the later value wins, so the dict is {'a': 2} with length 1. Keys compare by equality, which also merges 1, 1.0 and True into one key.

🛠️ Try it yourself in the interactive editor →

Q8. What is the difference between a ValueError and a TypeError?

TypeError: the argument's type is wrong for the operation — int(None). ValueError: the type is acceptable but the value is not — int('abc'). Interviewers use this to test whether you read exceptions or just catch them.

🛠️ Try it yourself in the interactive editor →

Q9. What does print(i) show after 'for i in range(3): pass'?

2 — loop variables are not block-scoped in Python; the name survives with its last value (range's stop is excluded, so never 3). Only an empty iterable leaves the name undefined and raises NameError.

🛠️ Try it yourself in the interactive editor →

Q10. Why do all instances share a list defined in the class body?

Attributes assigned in the class body belong to the class, and instances read them through lookup — one shared object. Mutations through any instance are visible to all. Per-instance state must be created in __init__ with self.x = [].

🛠️ Try it yourself in the interactive editor →

The pattern behind all ten

Every question above reduces to one of four language rules: evaluation happens once (defaults, class bodies), names share objects (aliasing, chained assignment), in-place returns None (sort/reverse/append), and control flow has exact sequencing (for/else, finally, except order). Interviewers rotate the surface syntax; the four rules underneath never change. Practise until you see the rule through the syntax — that is the whole trick.

Start the Interview Practice path →