ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
class Base:
def run(self): return self.step()
def step(self): return "base"
class Child(Base):
def step(self): return "child"
class Grand(Child):
def run(self): return "G" + super().run()
print(Grand().run(), Base.run(Child()))- A. Gbase base
- B. Gchild base
- C. Gbase child
- D. Gchild child
Correct answer: D. Gchild child
Base.run calls self.step(), which is resolved on the actual object (dynamic dispatch). For a Grand or a Child instance that is Child.step, even when run is invoked through Base.
More Python Programming questions
- What is printed? python class Meta(type): def new(m, name, bases, ns): ns["kind"] = name.upper() return super().new(m, name, bases, ns)…
- What is printed? python class S: slots = ("a",) class T(S): pass t = T() t.a = 1 t.b = 2 s = S() try: s.b = 2 except AttributeError…
- What is printed? python class Account: rate = 0.05 def init(self, bal): self.bal = bal def interest(self): return self.bal self.rate a =…
- What is printed? python def countergen(): c = 0 while True: cmd = yield c if cmd == "reset": c = 0 else: c += 1 g = countergen()…
- What is printed? python import itertools c = itertools.count() data = ["a", "b", "c"] pairs = list(zip(c, data)) print(pairs, next(c)…
- What is printed? (Output lines are shown separated by " / ".) python def f(x): return x 2 g = f del f print(g(3), g.name) try: f(1) except…