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())- A. 5.0 10.0
- B. 2.0 2.0
- C. 5.0 2.0
- 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
- What is printed? python def countergen(): c = 0 while True: cmd = yield c if cmd == "reset": c = 0 else: c += 1 g = countergen()…
- What is printed? python import itertools c = itertools.count() data = ["a", "b", "c"] pairs = list(zip(c, data)) print(pairs, next(c)…
- What is printed? (Output lines are shown separated by " / ".) python def f(x): return x 2 g = f del f print(g(3), g.name) try: f(1) except…
- What is printed? python x = [1, 2, 3] it = iter(x) next(it) x.insert(0, 0) print(next(it), list(it))
- What is printed? python words = ["apple", "Banana", "cherry", "date"] bylen = {} for w in words: bylen.setdefault(len(w), []).append(w)…
- What is printed? python def outer(): results = [] def rec(n): if n == 0: results.append("x") return for in range(n): rec(n - 1) rec(3)…