ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
class Stack(list):
    def push(self, v):
        self.append(v)
        return self
s = Stack()
s.push(1).push(2)
t = s + [3]
print(type(t).__name__, s, isinstance(s, list), s == [1, 2])
  1. A. Stack [1, 2] True True
  2. B. list [1, 2] True False
  3. C. list [1, 2] True True
  4. D. Stack [1, 2, 3] True True

Correct answer: C. list [1, 2] True True

Inherited list operations like + return a plain list, not the subclass. A Stack compares equal to a list with the same items.

More Python Programming questions

Exams that ask this topic