0% found this document useful (0 votes)
27 views

Recursion

The document discusses recursion through a series of multiple choice and code based questions. Recursion is defined as a function that calls itself. Key aspects covered include base conditions to end recursion, memory usage of recursive functions, and evaluation order of recursive calls. Examples of recursive functions calculating factorials, sums, and Fibonacci sequences are provided.

Uploaded by

jahringa123
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Recursion

The document discusses recursion through a series of multiple choice and code based questions. Recursion is defined as a function that calls itself. Key aspects covered include base conditions to end recursion, memory usage of recursive functions, and evaluation order of recursive calls. Examples of recursive functions calculating factorials, sums, and Fibonacci sequences are provided.

Uploaded by

jahringa123
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

RECURSION

1 Which is the most appropriate definition for recursion?


a) A function that calls itself
b) A function execution instance that calls another execution instance of the same function
c) A class method that calls another class method
d) An in-built method that is automatically called
An b)
s
2 To end the recursive function, it must include
An base condition
s
3 What happens if the base condition isn’t defined in recursive programs?
a) Program gets into an infinite loop
b) Program runs once
c) Program runs n number of times where n is the argument given to the function
d) An exception is thrown
An c)
s
4 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
An c)
s
5 Execution of recursive calls in recursion is in which order (Choose correct option)
1. Sequential
2. Reverse
An 2.
s
6 Define a recursive SCREENSAVER() function which displays “Welcome to my PC”
infinite times.
An def SCREENSAVER()
s Print(“Welcome to my PC”)
SCREENSAVER()
7 Fill in the line of the following Python code for calculating the factorial of a number.
def fact(n):
if n==1:
return 1
else:
return
a) n * fact(n-1)
b) (n-1) * (n-2)
c) n * (n-1)
d) fact(n) * fact(n-1)
An a)
s
8 Define a recursive function FACT(n) to calculate and return the factorial of n
An def FACT(n): if n==1:
s return 1 else:
return n * FACT(n-1)

9 Define a recursive function SUM1TON(n) to calculate sum of all the number from 1 to n
An def sum(n): if n==1:
s return 1 else:
return n+sum(n-1)

10 Define a recursive function FIBO(n) to generate Fibonacci series for first n numbers For e.g. if n is
6, the numbers to generate are 0 1 1 2 3 5
An def fibo(n):
s if n<=1:
return n
else: return fibo(n-1)+fibo(n-2)

11 Find the output of following Python code:


def Func1(A,B):
if A % B == 0:
return 10
else:
return A + Func1(A,B-1)
val = Func1(20,15)
print(val)
An 110
s
12 Find the output of following Python code:
def Alter(n):
if n<=2:
return 5
else:
return n + Alter(n-2)

sum =
Alter(20)
print(sum)
An 113
s
13 Find the output of following Python code:
def A(n):
if n==1:
return 1
else:
return n + B(n-1)
def B(n):
if n==1:
return 5
else:
return n + A(n-1)

val =
A(10)
print(val)
An 59
s
14 Find the output of following Python code:
def fun(x):
if(x > 0):
x -= 1
fun(x)
print(x , end=" ")
x -= 1
fun(x)

a=4
fun(
a)
An 0120301
s
15 Find the output of following Python code:
def fun(n):
if n==4:
return n
else:
return 2*fun(n+1)
x = fun(2)
print(x)
An 16
s
16 Find the output of following Python code:
def fun(x,y):
if x==0:
return y
else:
return fun(x-1,x+y)
a=
fun(4,3)
print(a)
An 13
s
17 Find the output of following Python code:
def fun(n):
if n==0:
return print(n
%2,end='') fun(n//2)

fun(25)

An 10011
s
18 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
An c)
s
19 Which of these is not true about recursion?
a) It‟s easier to code some real-world problems using recursion than non-recursive equivalent
b) Recursive functions are easy to debug
c) Recursive calls take up a lot of memory
d) Programs using recursion take longer time than their non-recursive equivalent
An b)
s
20 Find the output of the following Python code:
def fun1(msg,length):
if length==0:
return
else:
print(msg[length-1],end='')
fun1(msg,length-1)
fun1('amit',4)
An tima
s

You might also like