Friday , September 20 2024

What is the expected effect of running the following code? class A: def __init__(self, v): self.__a = v + 1 a = A(0) print(a.__a)

Questions BankCategory: Python Essentials 2What is the expected effect of running the following code? class A: def __init__(self, v): self.__a = v + 1 a = A(0) print(a.__a)
What is the expected effect of running the following code?

class A:
    def __init__(self, v):
        self.__a = v + 1
 
 
a = A(0)
print(a.__a)
  • The code will print 1
  • The code will print 2
  • The code will raise an AttributeError exception
  • The code will print 0

Explanation: An attempt to execute the code will result in the following error message: AttributeError: ‘A’ object has no attribute ‘__a’. Because __a is a private variable (note the two underscores), it cannot be accessed from outside the class.

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