ProgrammingModerateGATE CS · Product-company placement
What is printed?
What is printed?
class Shape:
sides = 0
def describe(self):
return f"{type(self).__name__}:{self.sides}"
class Tri(Shape):
sides = 3
print(Shape().describe(), Tri().describe(), Tri.describe is Shape.describe)- A. Shape:0 Tri:3 True
- B. Shape:0 Shape:3 True
- C. Shape:0 Tri:0 True
- D. Shape:0 Tri:3 False
Correct answer: A. Shape:0 Tri:3 True
The inherited method looks up self.sides and type(self) on the actual object, so a Tri reports Tri:3. Accessing the function through either class gives the same plain function object.
More Python Programming questions
- What is printed? python class Money: def init(self, v): self.v = v def lt(self, o): return self.v < o.v def repr(self): return…
- What is printed? python class A: def eq(self, o): print("A.eq", end=" ") return NotImplemented class B(A): def eq(self, o): print("B.eq"…
- What is printed? python class Lazy: def init(self): self.calls = 0 @property def value(self): self.calls += 1 return 42 from functools…
- What is printed? python class Pos: def setname(self, owner, name): self.name = "" + name def get(self, obj, typ=None): return getattr(obj…
- What is printed? python def f(n): return [i for i in range(n) if i % 3 == 0 if i % 2 == 0] g = lambda n: [i for i in range(n) if i % 3 ==…
- What is printed? python a = {"x": 1, "y": 2} b = {v: k for k, v in a.items()} c = {k: v for k, v in zip("abc", [1, 2])} print(b, c, {1…