ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
class P:
pass
p = P()
p.n = 1
p.n += 1
P.m = 10
q = P()
q.m += 5
print(p.n, P.m, q.m, p.m, vars(q))- A. 2 15 15 15 {'m': 15}
- B. 2 10 15 10 {}
- C. 2 15 15 10 {'m': 15}
- D. 2 10 15 10 {'m': 15}
Correct answer: D. 2 10 15 10 {'m': 15}
q.m += 5 reads the class attribute (10) and writes an instance attribute on q, leaving P.m at 10. vars(q) shows q's instance dict.
More Python Programming questions
- What is printed? python def makeacc(): total = [] def add(x): total.append(x) return sum(total) def reset(): nonlocal total total = []…
- What is printed? python adders = [lambda x, n=n: x + n for n in range(3)] mults = list(map(lambda n: (lambda x: x n), range(3)))…
- What is printed? python def outer(): fs = [] for i in range(3): def f(j=None): return i if j is None else j fs.append(f) if i == 1: break…
- What is printed? python x = "global" def f(): return [x for in range(1)][0] def g(): x = "local" return (lambda: x)() def h(): return…
- What is printed? python try: r = [1 / x for x in [1, 0, 2]] except ZeroDivisionError: r = "partial?" g = (1 / x for x in [1, 0, 2]) out =…
- What is printed? python def f(x): if x > 0: return x print(f(1), f(-1), f(0) or "zero", f(2) and f(-2))