ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
class A: pass
class B(A): pass
try:
class C(A, B): pass
print("ok", end=" ")
except TypeError:
print("TypeError", end=" ")
class D(B, A): pass
print([k.__name__ for k in D.__mro__])- A. ok ['D', 'B', 'A', 'object']
- B. TypeError ['D', 'A', 'B', 'object']
- C. TypeError ['D', 'B', 'A', 'object']
- D. ok ['D', 'B', 'A', 'A', 'object']
Correct answer: C. TypeError ['D', 'B', 'A', 'object']
C3 linearisation must keep every subclass before its bases and respect the order of the bases list. class C(A, B) asks for A before B, but B is a subclass of A and must come first, so no consistent MRO exists and Python raises TypeError. D(B, A) is consistent: D, B, A, object, each class appearing once.
More Python Programming questions
- What is printed? python class R: def iter(self): print("it", end=" ") return iter([1, 2, 3]) r = R() print(2 in r, 5 in r)
- What is printed? python d = {1: "a", 2: "b"} for k in d: d[k] = d[k] 2 try: for k in d: d[k 10] = "x" except RuntimeError: print("RE"…
- What is printed? python s = "abcbaXabcba" print(s.strip("ab"), s.removeprefix("ab"), s.lstrip("cba"))
- What is printed? python print(-7.5 // 2, -7.5 % 2, 7 // -2.0, 7 % -2.0)
- What is printed? python n = [1] print(1 in n == True, (1 in n) == True, 0 < 1 in n)
- What is printed? python class P: slots = ("x",) def init(self): self.x = 1 p = P() try: p.y = 2 except AttributeError: print("AE", end="…