Recursion

Recursion Functions


When a function calls itself is known as recursion. Recursion works like loop but sometimes it makes more sense to use recursion than loop. You can convert any loop to recursion.

A recursive function calls itself. Imagine a process would iterate indefinitely if not stopped by some condition! Such a process is known as infinite iteration. The condition that is applied in any recursive function is known as base condition. A base condition is must in every recursive function otherwise it will continue to execute like an infinite loop.

Overview of how recursive function works

1. Recursive function is called by some external code.

2. If the base condition is met then the program gives meaningful output and exits.

3. Otherwise, function does some required processing and then calls itself to continue recursion.

Here is an example of recursive function used to calculate factorial.

Example :

def fact(n):
if n == 0:
return 1
else:
return n * fact (n-1)
print (fact (0))
print (fact (5))

Output:

1

120

Qus. 1 : Which is the most appropriate definition for recursion?

  1. A function that calls itself
  2. A function execution instance that calls another execution instance of the same function
  3. A class method that calls another class method
  4. An in-built method that is automatically called
Qus. 2 : Which of these is false about recursion?

  1. Recursive function can be replaced by a non-recursive function
  2. Recursive functions usually take more memory space than non-recursive function
  3. Recursive functions run faster than non-recursive function
  4. Recursion makes programs easier to understand
Qus. 3 :

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 _____________________


  1. num*fact(num-1)
  2. (num-1)*(num-2)
  3. num*(num-1)
  4. fact(num)*fact(num-1)
Qus. 4 :

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


  1. 13
  2. 7
  3. Infinite loop
  4. 17
Qus. 5 :

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


  1. 011
  2. 110
  3. 3
  4. Infinite loop
Qus. 6 : What is tail recursion?

  1. A recursive function that has two base cases
  2. A function where the recursive functions leads to an infinite loop
  3. A recursive function where the function doesn’t return anything and just prints the values
  4. A function where the recursive call is the last thing executed by the function
Qus. 7 :

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)


  1. Both a() and b() aren’t tail recursive
  2. Both a() and b() are tail recursive
  3. b() is tail recursive but a() isn’t
  4. a() is tail recursive but b() isn’t
Qus. 8 : Which of the following statements is false about recursion?

  1. Every recursive function must have a base case
  2. Infinite recursion can occur if the base case isn’t properly mentioned
  3. A recursive function makes the code easier to understand
  4. Every recursive function must have a return value
Qus. 9 :

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


  1. 50
  2. 100
  3. 74
  4. Infinite loop
Qus. 10 : What happens if the base condition isn’t defined in recursive programs?

  1. Program gets into an infinite loop
  2. Program runs once
  3. Program runs n number of times where n is the argument given to the function
  4. An exception is thrown
Qus. 11 : Which of these is not true about recursion?

  1. Making the code look clean
  2. A complex task can be broken into sub-problems
  3. Recursive calls take up less memory
  4. Sequence generation is easier than a nested iteration
Qus. 12 : Which of these is not true about recursion?

  1. It’s easier to code some real-world problems using recursion than non-recursive equivalent
  2. Recursive functions are easy to debug
  3. Recursive calls take up a lot of memory
  4. Programs using recursion take longer time than their non-recursive equivalent
Qus. 13 :

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


  1. 0 1 2 3
  2. An exception is thrown
  3. 0 1 1 2 3
  4. 0 1 1 2
Qus. 14 : <p>What will be the output of the following?</p><pre>def iq(a,b):<br>&nbsp; &nbsp; if(a==0):<br>&nbsp; &nbsp; &nbsp; &nbsp;return b<br>&nbsp; &nbsp; else:<br>&nbsp; &nbsp; &nbsp; &nbsp;return iq(a-1,a+b)<br>print(iq(3,6))</pre>

  1. 9
  2. 10
  3. 11
  4. 12
Qus. 15 : The sequence logic will not be used while

  1. Subtracting two numbers
  2. Comparing two data values
  3. Providing output to the user
  4. Adding two numbers
Qus. 16 : Recursive function is

  1. A function that calls itself
  2. A function that calls other functions
  3. Both (A) and (B)
  4. None of the above
Qus. 17 : What is a recursive function?

  1. A function that calls other function.
  2. A function which calls itself.
  3. Both A and B
  4. None of the above
Qus. 18 : What is recursion in python?

  1. Repeating a process in a loop
  2. Calling a function from within itself
  3. Using a loop to iterate over elements
  4. Breaking down a problem into smaller subproblems

Programs

Python program to convert decimal into other number systems

View Solution


python program that generates six random numbers in a sequence created with start stop step

View Solution


Python program to illustrate the global variable

View Solution


python program to create a function to calculate factorial value

View Solution


Python program to find the sum of factorial upto n terms using function

View Solution


python program to create a function for reverse the number

View Solution


Python program to check number is Palindrome or not using function

View Solution


Python program to create a function for calculating sum of digit

View Solution


Python program to create recursive function to find factorial

View Solution


python function to accept 2 number and return addition subtraction multiplication and division

View Solution


python function to accept number of days and return week and days to its equivalent

View Solution


python function to extract the day month year from given date

View Solution


Python function to find the sum of all numbers between 100 and 500 which are divisible by 2

View Solution


Python function to get two matrices and multiply them

View Solution


python recursive function to find the sum of digits

View Solution


python recursive function to print the reverse of number

View Solution


Write a function with name dividebyfive which generate and prints a random integer number from the range 0 to 100 and then return True if the randomly generated number is divisible by 5, and False otherwise.

View Solution


Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad