Q.

What will be the output of the following
Python code?
1. class tester:
2. def __init__(self, id):
3. self.id = str(id)
4. id="224"
5.
6. >>>temp = tester(12)
7. >>>print(temp.id)

A. 224
B. error
C. 12
D. none
Answer» C. 12
Explanation: id in this case will be the attribute of the class.
1.1k
1
Do you find this helpful?
4

Discussion

coder developer
1 year ago

In the given code, you have a class tester with an __init__ method that takes an id parameter and assigns the string representation of that id to the instance variable self.id. However, inside the constructor, you also have a local variable id that is assigned the string "224". This local variable does not affect the instance variable self.id because it is a separate variable within the scope of the constructor.

When you create an object temp of the tester class with the parameter 12 and print temp.id, it will output: 12
0