Python practice · 15 questions

Python fundamentals: practice set

15 questions on the Python behaviour interviewers probe: references, mutable defaults, closures, inheritance, exceptions and generators. Most ask what a snippet prints, and every one of those answers was checked by running the code. At the end you get a score per topic.

  1. Question 1 of 15 · Core syntax & dataWhat does this print?
    Python
    a = [1, 2, 3]
    b = a
    b.append(4)
    print(a)
  2. Question 2 of 15 · Core syntax & dataWhat does this print?
    Python
    print(0.1 + 0.2 == 0.3)
  3. Question 3 of 15 · Core syntax & dataWhat does this print?
    Python
    d = {'a': 1, 'b': 2, 'a': 3}
    print(d)
  4. Question 4 of 15 · Core syntax & dataWhat does this print?
    Python
    s = 'pipeline'
    print(s[-4:], s[::-1][:3])
  5. Question 5 of 15 · FunctionsWhat does this print?
    Python
    def add(x, items=[]):
        items.append(x)
        return items
    
    add(1)
    print(add(2))
  6. Question 6 of 15 · FunctionsWhat does this print?
    Python
    print([x * 2 for x in range(5) if x % 2])
  7. Question 7 of 15 · FunctionsWhat does this print?
    Python
    funcs = [lambda: i for i in range(3)]
    print([f() for f in funcs])
  8. Question 8 of 15 · FunctionsWhat does this print?
    Python
    def f(a, *args, **kwargs):
        return len(args), sorted(kwargs)
    
    print(f(1, 2, 3, x=4))
  9. Question 9 of 15 · OOPWhat does this print?
    Python
    class Batch:
        rows = []
    
    a, b = Batch(), Batch()
    a.rows.append(1)
    print(b.rows)
  10. Question 10 of 15 · OOPWhat does this print?
    Python
    class A:
        def who(self): return 'A'
    
    class B(A):
        def who(self): return 'B' + super().who()
    
    class C(A):
        def who(self): return 'C' + super().who()
    
    class D(B, C):
        pass
    
    print(D().who())
  11. Question 11 of 15 · OOPWhat does this print?
    Python
    from dataclasses import dataclass
    
    @dataclass
    class Point:
        x: int
        y: int
    
    class Plain:
        def __init__(self, x, y):
            self.x, self.y = x, y
    
    print(Point(1, 2) == Point(1, 2), Plain(1, 2) == Plain(1, 2))
  12. Question 12 of 15 · ExceptionsWhat does this print?
    Python
    def f():
        try:
            return 'try'
        finally:
            print('finally', end=' ')
    
    print(f())
  13. Question 13 of 15 · ExceptionsWhat does this print?
    Python
    try:
        int('x')
    except Exception:
        print('general')
    except ValueError:
        print('value')
  14. Question 14 of 15 · Generators & decoratorsWhat does this print?
    Python
    g = (x for x in range(3))
    print(sum(g), sum(g))
  15. Question 15 of 15 · Generators & decoratorsWhat does this print?
    Python
    def double(fn):
        def wrapper(*args):
            return fn(*args) * 2
        return wrapper
    
    @double
    def inc(x):
        return x + 1
    
    print(inc(3), inc.__name__)
0 of 15 answered

Revise the topics in the Python guide.