What will be the output of the following Python code? [code] class A: def __str__(self): return '1' class B(A): def __init__(sel

Khánh Đoan

New member
What will be the output of the following Python code?
Code:
class A:
    def __str__(self):
        return '1'
class B(A):
    def __init__(self):
        super().__init__()
class C(B):
    def __init__(self):
        super().__init__()
def main():
    obj1 = B()
    obj2 = A()
    obj3 = C()
    print(obj1, obj2,obj3)
main()
A. 1 1 1
B. 1 2 3
C. ‘1’ ‘1’ ‘1’
D. An exception is thrown
 
Answer: a
Explanation: The super().__init__() in the subclasses has been properly invoked and none of other subclasses return any other value. Hence 1 is returned each time the object is created and printed.
 

Tìm kiếm

Back
Top