LearnFrenzy
Sign in Take a free test

Recursion · Predict the Output · Question 11

Two functions calling each other Answer, explanation & discussion

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

What is the output of the following Python program?

main.py
def is_even(n):
    return True if n == 0 else is_odd(n - 1)

def is_odd(n):
    return False if n == 0 else is_even(n - 1)

print(is_even(10), is_odd(7), is_even(3))
  • A True True False Correct
  • B True False False
  • C False True True
  • D RecursionError
Correct Answer: Option A

The calls alternate, decreasing n by 1 each time, until one function reaches 0.

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