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)
  1. A. 2 2 True False
  2. B. 3 2 False False
  3. C. TypeError
  4. 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

Exams that ask this topic