ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
class K:
x = 10
def method(self):
x = 20
return x
print(K().method(), K.x)- A. 10 10
- B. 20 20
- C. 20 10
- D. 10 20
Correct answer: C. 20 10
Inside method, x = 20 creates a plain local variable that shadows the class attribute entirely (class attributes are never implicitly visible inside methods without self./cls.), so method() returns 20, while the class attribute K.x remains 10.
More Python Programming questions
- What is printed? python class Counter: def init(self, n): self.n = n def iter(self): self.i = 0 return self def next(self): if self.i >=…
- What is printed? python class Box: def init(self, items): self.items = items def len(self): return len(self.items) def contains(self, x)…
- What is printed? python class D: def getitem(self, key): if isinstance(key, slice): return f"slice {key.start}:{key.stop}:{key.step}"…
- What is printed? python try: try: 1 / 0 except ZeroDivisionError: raise RuntimeError("r") except RuntimeError as e: print(e…
- What is printed? python try: raise ValueError("x") from None except ValueError as e: print(e.cause, e.suppresscontext)
- What is printed? python try: try: raise KeyError("k") except ValueError: print("vl") except KeyError: print("kl")