Recursion in Python Quiz

    Last Updated :
    Discuss
    Comments

    Question 1

    Which is the most appropriate definition for recursion?

    • A class method that calls another class method 

    • A function execution instance that calls another execution instance of the same function

    • An in-built method that is automatically called 

    • A function that calls itself

    Question 2

    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_______________

    • (num-1)*(num-2)

    • num*fact(num-1)

    • fact(num)*fact(num-1)

    • num*(num-1)

    Question 3

    Which of these is false about recursion? 

    • Recursive functions run faster than non-recursive function

    • Recursive function can be replaced by a non-recursive function

    •  Recursive functions usually take more memory space than non-recursive function

    • Recursion makes programs easier to understand View

    Question 4

    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="")


     

    • Infinite loop

    • 011

    • 3

    • 110

    Question 5

    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))

    • 13

    • 17

    • 7

    • Infinite Loop

    Question 6

    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)

    • Both a() and b() aren’t tail recursive

    • b() is tail recursive but a() isn’t

    • a() is tail recursive but b() isn’t

    • Both a() and b() are tail recursive

    Question 7

    Which of the following statements is false about recursion ? 

    • Every recursive function must have a base case

    • Every recursive function must have a return value

    • Infinite recursion can occur if the base case isn’t properly mentioned

    • A recursive function makes the code easier to understand

    Question 8

    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))

    • 100

    •  50

    • 74

    • Infinite loop

    Question 9

    Which of these is not true about recursion?

    • Making the code look clean

    • Recursive calls take up less memory

    • A complex task can be broken into sub-problems

    • Sequence generation is easier than a nested iteration

    Question 10

    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=" ")

    • 0 1 1 2

    • 0 1 2 3

    • An exception is thrown

    • 0 1 1 2 3

    There are 10 questions to complete.

    Take a part in the ongoing discussion