ProgrammingHardTCS NQT · Wipro pattern
What is printed?
What is printed?
class A:
x = 1
def get(self):
return self.x
a = A()
a.x = 5
A.x = 9
b = A()
print(a.get(), b.get(), A.x)- A. 5 1 9
- B. 5 9 9
- C. 9 9 9
- D. 5 9 1
Correct answer: B. 5 9 9
An instance attribute shadows the class attribute for that object only. a keeps its own x = 5, while b reads the class value that was changed to 9.
More Python Programming questions
- What is printed? python class A: def f(self): return ['A'] class B(A): def f(self): return ['B'] + super().f() class C(A): def f(self)…
- What is printed? python from itertools import product print(len(list(product('01', repeat=3))), list(product('ab', 'x'))[-1], sum(1 for in…
- What is printed? python import heapq, itertools c = itertools.count() h = [] for p, name in [(2, 'x'), (1, 'y'), (2, 'z'), (1, 'w')]…
- What is printed? python d = dict.fromkeys(['a', 'b'], []) d['a'].append(1) print(d, d['b'] is d['a'])
- What is printed? python def pc(x): c = 0 while x: x &= x - 1 c += 1 return c print(pc(0b1011011), pc(255), pc(0))
- What is printed? python def rev8(x): r = 0 for in range(8): r = (r << 1) | (x & 1) x >>= 1 return r print(rev8(1), rev8(0b00010110)…