ProgrammingModerateGATE CS · Product-company placement

What is printed?

What is printed?
class Account:
    rate = 0.05
    def __init__(self, bal): self.bal = bal
    def interest(self): return self.bal * self.rate
a = Account(100)
b = Account(100)
b.rate = 0.1
Account.rate = 0.02
print(a.interest(), b.interest())
  1. A. 5.0 10.0
  2. B. 2.0 2.0
  3. C. 5.0 2.0
  4. D. 2.0 10.0

Correct answer: D. 2.0 10.0

a has no own rate, so it sees the class attribute at call time (now 0.02). b has an instance attribute rate = 0.1 that shadows the class one.

More Python Programming questions

Exams that ask this topic