Friday , September 20 2024

What will be the result of executing the following code? class A: def __str__(self): return ‘a’ class B: def __str__(self): return ‘b’ class C(A, B): pass o = C() print(o)

Questions BankCategory: Python Essentials 2What will be the result of executing the following code? class A: def __str__(self): return ‘a’ class B: def __str__(self): return ‘b’ class C(A, B): pass o = C() print(o)
What will be the result of executing the following code?

class A:
    def __str__(self):
        return 'a'
 
 
class B:
    def __str__(self):
        return 'b'
 
 
class C(A, B):
    pass
 
 
o = C()
print(o)
  • it will print c
  • it will print b
  • it will print a
  • it will raise an exception

Explanation: The code will print a to the screen, because the class named first in the multiple inheritance path (in this case, class C(A, B):) passes its attribute value to the child class (in this case, class C).

The code will not raise an exception, because it’s compatible with the MRO rules.

More Questions: Python Essentials 2 – Module 3 Test