Test Series - python

Test Number 71/108

Q: Which of these is false about recursion?
A. Recursive function can be replaced by a non-recursive function
B. Recursive functions usually take more memory space than non-recursive function
C. Recursive functions run faster than non-recursive function
D. Recursion makes programs easier to understand
Solution: The speed of a program using recursion is slower than the speed of its non-recursive equivalent.
Q: Fill in the line of the following Python code for calculating the factorial of a number.

def fact(num):
    if num == 0: 
        return 1
    else:
        return _____________________
A. num*fact(num-1)
B. (num-1)*(num-2)
C. num*(num-1)
D. fact(num)*fact(num-1)
Solution: Suppose n=5 then, 5*4*3*2*1 is returned which is the factorial of 5.
Q: What will be the output of the following Python code?

def test(i,j):
    if(i==0):
        return j
    else:
        return test(i-1,i+j)
print(test(4,7))
A. 13
B. 7
C. Infinite loop
D. 17
Solution: The test(i-1,i+j) part of the function keeps calling the function until the base condition of the function is satisfied.
Q: What will be the output of the following Python code?

l=[]
def convert(b):
    if(b==0):
        return l
    dig=b%2
    l.append(dig)
    convert(b//2)
convert(6)
l.reverse()
for i in l:
    print(i,end="")
A. 011
B. 110
C. 3
D. Infinite loop
Solution: The above code gives the binary equivalent of the number.
Q: What is tail recursion?
A. A recursive function that has two base cases
B. A function where the recursive functions leads to an infinite loop
C. A recursive function where the function doesn’t return anything and just prints the values
D. A function where the recursive call is the last thing executed by the function
Solution: A recursive function is tail recursive when recursive call is executed by the function in the last.
Q: Observe the following Python code?

def a(n):
    if n == 0:
        return 0
    else:
        return n*a(n - 1)
def b(n, tot):
    if n == 0:
        return tot
    else:
        return b(n-2, tot-2)
A. Both a() and b() aren’t tail recursive
B. Both a() and b() are tail recursive
C. b() is tail recursive but a() isn’t
D. a() is tail recursive but b() isn’t
Solution: A recursive function is tail recursive when recursive call is executed by the function in the last.
Q: Which of the following statements is false about recursion?
A. Every recursive function must have a base case
B. Infinite recursion can occur if the base case isn’t properly mentioned
C. A recursive function makes the code easier to understand
D. Every recursive function must have a return value
Solution: A recursive function needn’t have a return value.
Q: What will be the output of the following Python code?

def fun(n):
    if (n > 100):
        return n - 5
    return fun(fun(n+11));
 
print(fun(45))
A. 50
B. 100
C. 74
D. Infinite loop
Solution: The fun(fun(n+11)) part of the code keeps executing until the value of n becomes greater than 100, after which n-5 is returned and printed.
Q: Which of these is not true about recursion?
A. Making the code look clean
B. A complex task can be broken into sub-problems
C. Recursive calls take up less memory
D. Sequence generation is easier than a nested iteration
Solution: Recursive calls take up a lot of memory and time as memory is taken up each time the function is called.
Q: What will be the output of the following Python code?

def a(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return a(n-1)+a(n-2)
for i in range(0,4):
    print(a(i),end=" ")
A. 0 1 2 3
B. An exception is thrown
C. 0 1 1 2 3
D. 0 1 1 2
Solution: The above piece of code prints the Fibonacci series.

You Have Score    /10