December Code

Python for placements: interview questions by topic

Python turns up in placement preparation in two ways: as the language many students write their coding round in, and as a subject in its own right, asked the way campus tests ask about any language — a few lines of code and 'what does this print?'. Those questions are rarely about syntax. They are about the places where Python behaves differently from what a newcomer expects: a default value created once, a name that becomes local because of a later assignment, a list that two variables share, a class attribute that every object sees.

The interview questions cluster around the same places. In functions: how arguments are passed, what a default value really is, which scope a name belongs to, and what a closure remembers. In object-oriented Python: self, class versus instance attributes, the method resolution order behind super(), and the dunder methods that make objects work with len, + and ==. Lists, dictionaries, comprehensions, generators and exceptions follow the same pattern — short code, one precise behaviour to predict.

The pages below take one topic each, with answers you can run and one question exactly as the diagnostic asks it. Pages for the other Python topics will follow once each topic's question pool is deep enough to publish a sample without giving the diagnostic away; the diagnostic itself already draws from every Python topic in the bank.

New to this? How to prepare for placements — the method — and what the readiness test measures.

Python topics, one page each

2 of the 9 Python topics in the bank have a page so far; the diagnostic draws from all 9.

  1. Python functions interview questions

    Python functions as interviews test them: argument kinds, pass by reference, mutable defaults, LEGB scope, nonlocal, closures and decorators.

  2. Python OOP interview questions

    Python OOP as interviews test it: self, __init__ and __new__, class vs instance attributes, classmethod, __repr__, super(), private names and ABCs.

One question, exactly as the diagnostic asks it

From the Python bank — 30 questions across 9 topics. The answer is marked because this one is public; in a sitting you choose first, then see why each option is right or wrong.

Functions & Scope · mediumPY-011

What does this code print?

def add(item, bucket=[]):
    bucket.append(item)
    return bucket

add(1)
print(add(2))
  1. 1[2]
  2. 2[1]
  3. 3[2, 1]
  4. 4[1, 2]correct

A default value is evaluated once, when the def statement runs, so every call that omits bucket shares the same list. The first call appends 1 to it and the second appends 2, so the second call returns [1, 2]. [2] assumes a fresh list per call, [1] ignores the second append, and [2, 1] reverses the order of the appends. The standard fix is bucket=None and bucket = [] inside the function when it is None.

More on this topic: Python functions interview questions

Measure it

Reading the answers tells you what’s true. A diagnostic tells you what you get wrong.

10 Python questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.