ProgrammingHardAccenture pattern
What is printed?
What is printed?
class F:
@staticmethod
def add(a, b):
return a + b
@classmethod
def create(cls):
return cls()
print(F.add(2, 3))
f = F()
print(f.add(1, 1))- A. 5 2
- B. 5 TypeError
- C. TypeError 2
- D. 5 5
Correct answer: A. 5 2
A staticmethod receives no implicit self or cls, whether called on the class or an instance, so both F.add(2,3) and f.add(1,1) just run the plain function: 5 and 2.
More Python Programming questions
- What is printed? python class H: slots = ["a", "b"] h = H() h.a = 1 try: h.c = 2 except AttributeError: print("AttributeError") print(h.a)
- What is printed? python class K: x = 10 def method(self): x = 20 return x print(K().method(), K.x)
- What is printed? python class Counter: def init(self, n): self.n = n def iter(self): self.i = 0 return self def next(self): if self.i >=…
- What is printed? python class Box: def init(self, items): self.items = items def len(self): return len(self.items) def contains(self, x)…
- What is printed? python class D: def getitem(self, key): if isinstance(key, slice): return f"slice {key.start}:{key.stop}:{key.step}"…
- What is printed? python try: try: 1 / 0 except ZeroDivisionError: raise RuntimeError("r") except RuntimeError as e: print(e…