(12CH15) Quiz
What is the output of the following program? def foo (n, r) : if n == 0 : return 0 else : return foo(n - 1, n + r) print(foo(3, 0))
0
What does the following code display? def foo(n) : if n == 1 : return 1 else : return n + foo(n - 1) print(foo (5) )
15
What is the output of the following program? def main() : n = 0 foo(n) def foo(n) : if n < 5 : foo(n + 1) else : print(n) main()
5
What is the output of the following code? def foo(n) : if n >= 2 : print (n, end = ' ') foo(n - 1) foo(5)
5432
A function is called from the main function for the first time and then calls itself seven times. What is the depth of recursion?
7
Which of the following about Recursion is true?
A recursive function must have some way to control the number of times it repeats.
The below recursive function prints hello world 3 times: def showHello(n) : if n > 0 : print("hello world") showHello (n > 1) showHello(3)
False
The code below displays 0 1 2 3 4 5? def foo(n) : if n > 0 : foo (n - 1) print(n, end = ' ') foo(5)
False
What is the first step to take in order to apply a recursive approach?
Identify at least one case in which the problem can be solved without recursion.
Match the following: Infinite recursion - occurs when there is a runtime error in the code that causes the program running forever without reaching the base case. Direct recursion - is that the recursive function invokes itself directly. Indirect recursion - is for the recursive function to invoke another function, and then this function directly or indirectly invokes the recursive function.
Infinite recursion - occurs when there is a runtime error in the code that causes the program running forever without reaching the base case. Direct recursion - is that the recursive function invokes itself directly. Indirect recursion - is for the recursive function to invoke another function, and then this function directly or indirectly invokes the recursive function.
In the program below, identify the line# that is causing the program to run infinitely: 1. def foo (n) : 2. if n != 0 : 3. print (n) 4. foo(n / 10) 5. foo(12345)
Line 4 has an error and should be changed to: foo(n // 10)
Which of the following about Recursion is NOT true?
Recursion is sometimes required to solve certain types of problems.
Which of the following statements is false?
Recursive functions run faster than non-recursive functions.
Tail recursion is desirable because, the function ends when the last recursive call ends, so there is no need store the intermediate calls in the stack, leading to reduced stack space.
True
The code below displays 5 4 3 2 1?
True
Which of the following describes the base case in a recursive solution?
a case in which the problem can be solved without recursion
Which of the following recursive functions can replace the below iterative function that performs the same operation? def foo(n) : while n > 0 : print(n) n -= 1 foo(3)
def foo(n) : if n > 0 : print(n) foo (n- 1) foo(3)
If, in a recursive solution, function A calls function B which calls function C, this is called ________ recursion.
indirect
Recursion is __________.
never required to solve a problem
A __________ function has no pending operations after a recursive call.
tail-recursive
