‹ Python tests

Python practice questions

10 questions in 1 test. Open one to answer it on its own, or take a whole test.

  1. A helper is defined as def f(x, acc=[]): acc.append(x); return acc and then called with print(f(1), f(2)) in a fresh interpreter. What does the print call output? · Functions & Arguments
  2. A billing check does if billed_count is expected_count: where both values are integers computed at runtime. Unit tests with counts like 5 and 42 always pass, but in production… · Data Model & Identity
  3. A caching layer stores responses in a dict whose keys are built from request details. One of the following keys makes the assignment cache[key] = response raise TypeError… · Data Model & Identity
  4. What does this code print? a = [[1], [2]]; b = a.copy(); b[0].append(9); b.append([3]); print(a) · Data Model & Identity
  5. On CPython 3.12, what does this code print? d = {}; d['b'] = 1; d['a'] = 2; d['c'] = 3; del d['a']; d['a'] = 4; d['b'] = 9; print(list(d)) · Built-in Types
  6. A developer builds a generator expression once and passes it to two aggregate functions: g = (x * x for x in range(3)); print(sum(g), sum(g)). What is printed? · Iterators & Generators
  7. A team's logging decorator is def log_calls(fn): containing an inner def wrapper(*args, **kwargs): that logs and returns fn(*args, **kwargs), and it returns wrapper. After… · Functions & Arguments
  8. A service scores images with a pure-Python, CPU-bound function (no I/O, no C extensions). It runs on an 8-core machine using the standard CPython 3.12 build. Moving the work from… · Concurrency
  9. A function f() has four clauses: a try: block that runs print('T'), an except ValueError: block that runs print('E'), an else: block that runs print('L') and then return 'R', and… · Exceptions
  10. On CPython 3.12, what does this code print? fs = [lambda: i for i in range(3)]; print([f() for f in fs]) · Scope & Closures