ProgrammingModerateCognizant GenC · TCS NQT
What is printed?
What is printed?
class A:
def __init__(self, v):
self.v = v
def __eq__(self, o):
return isinstance(o, A) and self.v == o.v
xs = [A(1), A(2)]
print(A(2) in xs, A(3) in xs, xs.index(A(2)), A(1) != A(1))- A. True False 1 True
- B. False False 1 False
- C. True False 0 False
- D. True False 1 False
Correct answer: D. True False 1 False
Python derives != from __eq__ when __ne__ is not defined, so A(1) != A(1) is False. The in and index tests use the same equality.
More Python Programming questions
- What is printed? python a, b = 5, 9 a ^= b b ^= a a ^= b print(a, b)
- What is printed? python nums = [10, 9, 100, 1] print(sorted(nums, key=str), max(nums, key=str), sorted(map(str, nums))[-1])
- What is printed? python def ngecirc(a): n = len(a) res = [-1] n st = [] for i in range(2 n): x = a[i % n] while st and a[st[-1]] < x…
- What is printed? python def dsum(n): return n if n < 10 else dsum(sum(map(int, str(n)))) print(dsum(9875), dsum(0), dsum(999999999999))
- What is printed? python def lcp(words): out = [] for col in zip(words): if len(set(col)) == 1: out.append(col[0]) else: break return…
- What is printed? python s = 'abc' 2 + 'd' print(s[::3], 'bcd' in s, s.index('c', 3))