What will be the output of the following code?
class A: def __init__(self,v = 1): self.v = v def set(self,v): self.v = v return v a = A() print(a.set(a.v + 1))
- 1
- 0
- 2
- 3
Explanation: Let’s analyze the code:
- The A class constructor creates an instance variable named v equal to the default value passed to the constructor’s parameter v, which is 1.
- The set method creates its own v variable, and returns the value passed to it.
- The a object is created, and the set method is called, which returns the v value equal to 1 + 1.
- The result 2 is printed to the console.
More Questions: Python Essentials 2 – Module 3 Test
Please login or Register to submit your answer