20 questions · untimed

Python interview: Python fundamentals round

Pick an option and check it. The explanation shows after each answer. Take as long as you like; nothing is timed and nothing is sent.

20 questions

Question 1 · easy

What does this Python 3.12 program print?

a = [1, 2]
b = a
b += [3]
c = a[:]
c.append(4)
print(a, len(c))

Question 2 · medium

What does this Python 3.12 program print?

import copy

grid = [[0, 0], [0, 0]]
shallow = copy.copy(grid)
deep = copy.deepcopy(grid)
shallow[0][0] = 1
deep[1][1] = 2
print(grid)

Question 3 · easy

In Python 3.12, consider:

a = [1, 2, 3]
b = [1, 2, 3]

Which statement about a == b and a is b is correct?

Question 4 · hard

What does this Python 3.12 program print?

t = ([1, 2], 3)
try:
    t[0] += [4]
    status = "ok"
except TypeError:
    status = "TypeError"
print(status, t)

Question 5 · easy

Why can the tuple (1, 2) be used as a dictionary key in Python 3.12 while the list [1, 2] cannot?

Question 6 · medium

A reviewer flags this Python 3.12 function:

def add_tag(tag, tags=[]):
    tags.append(tag)
    return tags

Which change is the idiomatic fix?

Question 7 · medium

What does this Python 3.12 program print?

def make_counter():
    count = 0
    def inc():
        nonlocal count
        count += 1
        return count
    return inc

c1 = make_counter()
c2 = make_counter()
c1()
c1()
print(c1(), c2())

Question 8 · medium

Calling add(5) in this Python 3.12 program raises UnboundLocalError. What is the cause, and what is the cleanest fix?

total = 0

def add(n):
    total += n
    return total

add(5)

Question 9 · easy

Given this Python 3.12 signature:

def f(a, *args, b=10, **kwargs):
    ...

What is true about the parameter b?

Question 10 · hard

The author expected this Python 3.12 program to print [0, 10, 20], but it prints [20, 20, 20]. What is the cause, and what is a correct fix?

funcs = []
for i in range(3):
    funcs.append(lambda: i * 10)
print([f() for f in funcs])

Question 11 · medium

What is the difference between __new__ and __init__ in a Python 3.12 class?

Question 12 · easy

What does this Python 3.12 program print?

class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y

    def __repr__(self):
        return f"Point({self.x}, {self.y})"

    def __str__(self):
        return f"({self.x}, {self.y})"

p = Point(1, 2)
print(p, [p])

Question 13 · medium

What does this Python 3.12 program print?

class Dog:
    tricks = []
    legs = 4

    def __init__(self, name):
        self.name = name

a, b = Dog("Rex"), Dog("Bo")
a.tricks.append("sit")
a.legs = 3
print(b.tricks, b.legs, Dog.legs)

Question 14 · hard

What does this Python 3.12 program print?

class A:
    def hello(self):
        return "A"
class B(A):
    def hello(self):
        return "B" + super().hello()
class C(A):
    def hello(self):
        return "C" + super().hello()
class D(B, C):
    def hello(self):
        return "D" + super().hello()
print(D().hello(), [k.__name__ for k in D.__mro__])

Question 15 · medium

In Python 3.12, what happens when the last line of this program runs?

class User:
    def __init__(self, uid):
        self.uid = uid

    def __eq__(self, other):
        return isinstance(other, User) and self.uid == other.uid

users = {User(1), User(1)}

Question 16 · easy

What does this Python 3.12 program print?

nums = map(lambda x: x * 2, [1, 2, 3])
print(list(nums), list(nums))

Question 17 · medium

What does this Python 3.12 program print?

def gen():
    print("start", end=" ")
    yield 1
    print("middle", end=" ")
    yield 2

g = gen()
print("created", end=" ")
print(next(g))

Question 18 · easy

Why should the inner wrapper of a decorator be decorated with @functools.wraps(func), as below (Python 3.12)?

import functools

def timed(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper

Question 19 · easy

This Python 3.12 program prints calling add and then None instead of 5. What is the fix?

def log_calls(func):
    def wrapper(*args, **kwargs):
        print("calling", func.__name__)
        func(*args, **kwargs)
    return wrapper

@log_calls
def add(a, b):
    return a + b

print(add(2, 3))

Question 20 · medium

In Python 3.12, what is the main difference between these two lines?

total_a = sum([n * n for n in range(10_000_000)])
total_b = sum(n * n for n in range(10_000_000))

Next: Coding round (Python)