Python Programming - Tricky Questions - Discussion

Discussion Forum : Tricky Questions - General Questions (Q.No. 1)
1.
What is the output of the following Python code?
def mystery_function(x):
    return x if x > 0 else mystery_function(-x)

result = mystery_function(-5)
print(result)
-5
5
0
This code will result in an infinite loop.
Answer: Option
Explanation:
The mystery_function recursively calls itself with the absolute value of the input until the input is greater than 0. Therefore, mystery_function(-5) returns mystery_function(5), resulting in the output 5.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.