ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
def f(n):
if n == 0:
return 0
return 1 + f(n - 1)
print(f(100))- A. 99
- B. 100
- C. 101
- D. RecursionError
Correct answer: B. 100
This counts down from 100 to 0, adding 1 at each level of recursion; it correctly computes 100, and Python's default recursion limit (around 1000) comfortably allows 100 levels of recursion.
More Python Programming questions
- What is printed? python d = {} d.setdefault("a", []).append(1) d.setdefault("a", []).append(2) d.setdefault("b", []).append(3) print(d)
- What is printed? python class A: def len(self): return 0 a = A() print(bool(a)) if a: print("truthy") else: print("falsy")
- What is printed? python x = 10 class C: x = 20 def m(self): return x c = C() print(c.m(), C.x)
- What is printed? python def f(x, y=10): def g(): return x + y y = 20 return g() print(f(1))
- What is printed? python nums = [1, 2, 3] doubled = list(map(lambda x: x 2, nums)) evens = list(filter(lambda x: x % 2 == 0, nums))…
- What is printed? python class A: def call(self, x): return x 2 a = A() print(a(5), callable(a))