Challenge - 5 Problems
Return Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
What is the output of this function call?
Consider the following Python function. What will be printed when
result = multiply_and_add(2, 3) is executed and then print(result) is called?Python
def multiply_and_add(x, y): product = x * y sum_ = x + y return product + sum_ result = multiply_and_add(2, 3) print(result)
Attempts:
2 left
๐ก Hint
Remember to add both the product and the sum of the two numbers.
โ Incorrect
The function multiplies 2 and 3 to get 6, adds 2 and 3 to get 5, then returns their sum: 6 + 5 = 11.
โ Predict Output
intermediate2:00remaining
What does this function return?
What is the output of the following code snippet?
Python
def check_even(num): if num % 2 == 0: return True else: return False print(check_even(7))
Attempts:
2 left
๐ก Hint
Check if 7 is divisible by 2 without remainder.
โ Incorrect
7 is not divisible by 2 evenly, so the function returns False.
โ Predict Output
advanced2:00remaining
What is the output of this recursive function?
What will be printed when the following code runs?
Python
def factorial(n): if n == 0: return 1 return n * factorial(n - 1) print(factorial(4))
Attempts:
2 left
๐ก Hint
Factorial of 4 is 4 * 3 * 2 * 1.
โ Incorrect
factorial(4) = 4 * factorial(3) = 4 * 3 * 2 * 1 = 24.
โ Predict Output
advanced2:00remaining
What is the value of x after this function call?
What is the value of
x after running the code below?Python
def add_to_list(lst, item): lst.append(item) return lst my_list = [1, 2] x = add_to_list(my_list, 3) print(x)
Attempts:
2 left
๐ก Hint
The function appends an item to the list and returns it.
โ Incorrect
The function adds 3 to the list [1, 2], so the returned list is [1, 2, 3].
โ Predict Output
expert2:00remaining
What is the output of this function with multiple return statements?
What will be printed when the following code runs?
Python
def test_return(x): if x > 0: return 'Positive' elif x == 0: return 'Zero' return 'Negative' print(test_return(-5))
Attempts:
2 left
๐ก Hint
Check the conditions for x = -5.
โ Incorrect
Since -5 is less than 0, the function returns 'Negative'.