PyMasterPython trace & interview prep
← Home

Python OOP Code Tracing

OOP questions test instance vs class attributes, method resolution, and __init__ side effects.

Example problems

Class variable

class A:
    x = 0
    def inc(self):
        A.x += 1
a, b = A(), A()
a.inc()
print(b.x)

Output: 1

Instance shadow

class B:
    val = 1
b = B()
b.val = 2
print(B.val, b.val)

Output: 1 2

Method chain

class C:
    def __init__(self, n):
        self.n = n
    def add(self, k):
        self.n += k
        return self
c = C(1).add(2).add(3)
print(c.n)

Output: 6

Generate 100+ more problems instantly

AI-powered unique questions for OOP classes at every difficulty.