ProgrammingVery hardHCL · Tech Mahindra · Python placement
What does the following Python 3 code print? class A: x = 10 def __init__(self): self.x = 20 a = A() print(a.x, A.x) A.x = 99 print(a.x, A.x)
- A. 20 10 20 99
- B. 20 20 99 99
- C. 20 10 99 99
- D. 10 10 99 99
Correct answer: A. 20 10 20 99
Instance attribute a.x=20 shadows the class attribute, so a.x stays 20 even when the class attribute A.x is later reassigned to 99.
More Python Programming questions
- What does the following Python 3 code print? class A: count = 0 def init(self): A.count += 1 a1 = A(); a2 = A(); a3 = A() print(A.count)
- What does the following Python 3 code print? print(round(2.5), round(3.5), round(2.675, 2))
- What does the following Python 3 code print? print(0.1 + 0.2 == 0.3)
- What does the following Python 3 code print? print(bool([]), bool({}), bool(()), bool(0.0), bool("0"))
- What does the following Python 3 code print? print([] == False, [] is False)
- What does the following Python 3 code print? x = [1, 2, 3] y = [1, 2, 3] print(x == y, x is y)