🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Comprehensions and Lambda: all 15 questions

Quiz: Comprehensions and Lambda · Question 13 of 15

What does this print? (code)

What does this print?

def total(n):
    if n == 0:
        return 0
    return n + total(n - 1)
print(total(4))
  1. A.4
  2. B.24
  3. C.10
  4. D.RecursionError
Answer & explanation

Correct answer: 10

4 + 3 + 2 + 1 + 0 = 10. The base case n == 0 stops the calls.

Can you get the rest right without peeking?

Take the timed Quiz: Comprehensions and Lambda quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →