LearnFrenzy
Sign in Take a free test

Recursion · Predict the Output · Question 3

Printing after the recursive call Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

What is the output of the following Python program?

main.py
def show(n):
    if n == 0:
        return
    show(n - 1)
    print(n, end=" ")

show(4)
print()
  • A 4 3 2 1
  • B 0 1 2 3 4
  • C 4
  • D 1 2 3 4 Correct
Correct Answer: Option D

The print happens after the deeper call returns, so the innermost call (n = 1) prints first.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links