Python MCQ test: 12 questions with answers (CodinGame, SkillValue type)
MCQPython12 questionsUpdated
This quiz is for developers preparing for a Python technical test (SkillValue, CodinGame, HackerRank) or a job interview. It contains 12 mixed-level questions, each with the correct answer and a detailed explanation of the trap.
In short
Python recruitment tests spend little time on basic syntax and a lot on how the interpreter actually behaves. The most frequent topics on SkillValue, CodinGame and HackerRank are mutable default arguments, shallow versus deep copies, variable scope and closures, generator consumption, and the complexity of list, dict and set operations. Questions are almost always phrased as "what does this program print?" with answer choices that differ by a single detail. You have to run the code in your head, line by line, rather than trust your first impression.
The topics that come up most
Mutable default arguments and *args / **kwargsA parameter's default value is evaluated once, when the function is defined. A default list or dict is therefore shared across every call, which makes it the most quoted Python pitfall in recruitment tests.
Lists, dicts, sets and copiesTests check whether you know when an assignment creates an alias and when it creates a new object. The classic trap: a shallow copy (slice, list(), copy()) still shares the nested lists with the original.
Slicing and comprehensionsNegative-step slicing, out-of-range slice bounds that do not raise, and dict comprehensions where several items map to the same key are regular sources of confusion.
is vs == and object identity== compares values, is compares identity (same object in memory). Tests use two lists with equal contents, or an alias, to see whether you tell them apart.
Scope, closures and decoratorsA variable assigned anywhere in a function is local for the whole function, hence UnboundLocalError. Lambdas created in a loop capture the variable, not its value, and a naive decorator replaces the original function's name.
Generators and complexityA generator can be consumed only once; sum(), list() or a for loop exhaust it. On complexity, you are expected to tell x in a list (O(n)) from x in a set (O(1) on average) and to know the cost of insert(0, x) or pop(0).
The default list is created once, when def runs, and reused on every call that omits items. The first call appends 1 and returns [1]; the second appends 2 to the same list and returns [1, 2]. [1] then [2] would require a fresh list per call, which is not what happens. No error is raised: the definition is valid, only its behavior is surprising. The idiomatic fix is items=None followed by items = [] if items is None else items.
What does this program print?
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b, a is b, a is c)
== compares contents: a and b hold the same elements, so True. is compares identity: a and b are two separate list objects built independently, so a is b is False. c = a copies nothing; it is a second name for the same object, so a is c is True. The other options mix up value equality with object identity.
What does this program print?
s = "python"
print(s[1:4], s[::-1], s[-2:])
s[1:4] takes indices 1, 2 and 3 (the upper bound is excluded): "yth". s[::-1] walks the string with a step of -1 and reverses it: "nohtyp". s[-2:] takes the last two characters in normal order: "on". The wrong options include index 4 in the slice, reverse the last slice too, or start at index 0 instead of 1.
Which of these operations runs in O(1) on average in Python?
A set is a hash table: membership testing is O(1) on average regardless of size. x in my_list and my_list.index(x) scan the list element by element, so O(n). my_list.insert(0, x) has to shift every existing element one slot, which is also O(n); for frequent insertions at the front, use collections.deque.
What does this program print?
import copy
a = [[1, 2], [3, 4]]
b = a[:]
c = copy.deepcopy(a)
a[0].append(99)
print(b[0], c[0])
a[:] is a shallow copy: b is a new list, but its elements are the very same sublist objects as in a. Mutating a[0] in place therefore also changes b[0], which becomes [1, 2, 99]. deepcopy recursively copies every sublist, so c[0] stays [1, 2]. The first option wrongly assumes slicing copies deeply; the second assumes deepcopy shares nested objects.
What does this program print?
funcs = [lambda: i for i in range(3)]
print([f() for f in funcs])
Each lambda captures the variable i, not its value at creation time. By the time the lambdas are called, the loop has finished and i is 2, the last value of range(3), hence [2, 2, 2]. [3, 3, 3] is wrong because range(3) stops at 2. There is no NameError: the comprehension variable remains reachable through the closure. To capture the current value, write lambda i=i: i.
count += 1 is an assignment, so Python treats count as a local variable of inc throughout the whole function body. When it reads count to compute count + 1, the local has no value yet: UnboundLocalError. The code is syntactically valid, so it is not a SyntaxError, and it prints neither 0 nor 1. You would need global count (or nonlocal in a nested function) to modify the enclosing variable.
A generator keeps its position and never rewinds. next(g) consumes the 1. sum(g) consumes the rest, 2 and 3, and returns 5. list(g) then hits an exhausted generator and returns an empty list. The options with 6 assume sum restarts from the beginning, and the one with [2, 3] forgets that sum already consumed everything.
a receives 1, the first positional argument. *args collects the remaining positionals into a tuple: (2, 3). b is a keyword-only parameter because it comes after *args; it is passed explicitly as b=4, so the default 10 is not used. **kwargs gathers undeclared keyword arguments into a dict: {'c': 5}. args is always a tuple, never a list, and c raises no error thanks to **kwargs.
@deco rebinds the name hello to the wrapper function returned by deco, so hello.__name__ is "wrapper" (functools.wraps would fix that). On the call, wrapper prints "before", calls the original hello, which returns a string without printing anything, prints "after", then returns the string; only then does the outer print display "Hello Ana". Options that put "Hello Ana" before "after" confuse return with print.
What does this program print?
d = {x % 3: x for x in range(10)}
print(d)
print(sorted({x % 3 for x in range(10)}))
In a dict comprehension, an existing key is simply overwritten with the new value. The possible keys are 0, 1 and 2; the last x for each key is 9 (9 % 3 == 0), 7 (7 % 3 == 1) and 8 (8 % 3 == 2). Key insertion order (0, 1, 2) is preserved, giving {0: 9, 1: 7, 2: 8}. The set comprehension keeps only the distinct remainders, sorted: [0, 1, 2]. Duplicate keys never raise an error in Python.
What happens when this program runs?
t = (1, [2, 3])
t[1] += [4]
print(t)
t[1] += [4] runs in two steps: first t[1].__iadd__([4]), which extends the list in place (it becomes [2, 3, 4]), then the assignment t[1] = result, which fails because a tuple does not support item assignment. The exception is raised after the mutation: the list has been changed, but the print never runs. It is a textbook case showing that += is not atomic and that tuple immutability applies to its references, not to the objects they point to.
Score0 / 12
On test day
Run every snippet in your head line by line and write down the state of each variable; the answer choices are designed to catch people who skim.
Whenever you see a list or dict as a default value, a slice of a nested list, or a lambda inside a loop, look for shared objects: that is almost always what the question is about.
Separate what is printed (print) from what is returned (return): many decorator and generator questions hinge on that ordering.
Review basic complexities before the test: list (append O(1), insert/pop at the front O(n), in O(n)), dict and set (lookup and in O(1) on average), sorted O(n log n).
What Python level do I need to pass this kind of MCQ?
Intermediate is enough: questions rarely target libraries, they target how the language itself works. Knowing the object model (references, mutability), generators and variable scope covers the vast majority of questions.
Which Python version do recruitment tests use?
Python 3, almost always 3.8 or newer. Tests no longer cover Python 2, although classic differences (print as a function, integer division, dict ordering guaranteed since 3.7) can still be the basis of a question.
How long is a Python MCQ on SkillValue or CodinGame?
Usually 15 to 40 minutes for 15 to 30 questions, so roughly one minute per question. Code-reading questions take longer than theory questions, which is why practicing fast mental execution pays off.
How do I improve after this quiz?
Go back to every question you missed and type the code into an interpreter to see the result, then tweak it to confirm your understanding. HackerRank exercises and CodinGame puzzles in Python complement this work on language pitfalls well.