ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class Matrix:
def __init__(self, r): self.r = r
def __getitem__(self, idx):
i, j = idx
return self.r[i][j]
def __contains__(self, v):
return any(v in row for row in self.r)
m = Matrix([[1, 2], [3, 4]])
print(m[1, 0], m[0, -1], 4 in m, 5 in m)- A. 2 2 True False
- B. 3 2 False False
- C. TypeError
- D. 3 2 True False
Correct answer: D. 3 2 True False
m[1, 0] passes the tuple (1, 0) as a single index. __contains__ is used for `in` when defined.
More Python Programming questions
- What is printed? python a = [1, 2, 3] b = a[:] print(a == b, a is b, a[:] is a[:], id(a) == id(b), a.copy() == list(a))
- A service catches errors with except Exception: around its main loop. Which of the following will NOT be caught by that handler?
- For a Python list a with n elements, which operation is O(n) in the worst case (not amortised O(1))?
- How many of these statements about CPython are correct? 1. A tuple is hashable only if all its elements are hashable. 2. x in someset is…
- In CPython, two threads each run a pure-Python CPU-bound loop (no I/O, no C extensions releasing the lock) on a 4-core machine. Compared…
- Which statement about iterables and iterators is correct?