ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
class C:
    def __init__(self): self.d = {}
    def __getattr__(self, name):
        return f"<{name}>"
    def __setattr__(self, name, v):
        object.__setattr__(self, name, v * 2 if isinstance(v, int) else v)
c = C()
c.x = 5
print(c.x, c.y, c.d, getattr(c, "z", "dflt"))
  1. A. 5 <y> {} dflt
  2. B. 10 <y> {} <z>
  3. C. 10 <y> {} dflt
  4. D. 10 AttributeError {} <z>

Correct answer: B. 10 <y> {} <z>

__getattr__ runs only when normal lookup fails, so c.x finds the stored 10 (doubled by __setattr__). getattr with a default only uses it if AttributeError is raised, but __getattr__ returns '<z>' instead.

More Python Programming questions

Exams that ask this topic