Friday , September 20 2024

What is the expected result of executing the following code? class A: def __init__(self): pass a = A(1) print(hasattr(a, ‘A’))

Questions BankCategory: Python Essentials 2What is the expected result of executing the following code? class A: def __init__(self): pass a = A(1) print(hasattr(a, ‘A’))
What is the expected result of executing the following code?

class A:
    def __init__(self):
        pass
 
 
a = A(1)
print(hasattr(a, 'A'))
  • The code will raise an exception
  • The code will print False
  • The code will print 1
  • The code will print True

Explanation: The code will raise a TypeError exception with the following error message: TypeError: __init__() takes 1 positional argument but 2 were given, because when object a of class A is being created, it calls the __init__ method with two arguments (self, and 1), while it requires only one (self).

More Questions: Python Essentials 2 (PE2) Course Final Test