ProgrammingModerateGATE CS & IT · Infosys
What is printed?
What is printed?
class S:
__slots__ = ('a',)
s = S()
s.a = 1
try:
s.b = 2
except AttributeError:
print('no dict', s.a)- A. no dict 2
- B. 2 1
- C. 1
- D. no dict 1
Correct answer: D. no dict 1
__slots__ removes the per-instance __dict__, so assigning a new attribute name raises AttributeError. The existing slot a keeps its value.
More Python Programming questions
- What is printed? python print(len(set(map(lambda x: x % 3, range(10)))), sum(1 for in filter(None, [0, 1, '', 'a', None])))
- What is printed? python def mpow(b, e, m): r = 1 b %= m while e: if e & 1: r = r b % m b = b b % m e >>= 1 return r print(mpow(2, 10…
- What is printed? python import math n = 1016 - 1 print(math.isqrt(n), int(n 0.5))
- What is printed? python print(sorted([('b', 2), ('a', 2), ('c', 3)], key=lambda t: (-t[1], t[0])))
- What is printed? python def ok(s): def chk(l, r): while l < r: if s[l] != s[r]: return False l += 1 r -= 1 return True l, r = 0, len(s)…
- What is printed? python def removek(num, k): st = [] for d in num: while k and st and st[-1] > d: st.pop() k -= 1 st.append(d) st =…