Here is class 12 computer science Unit 6 [Type A] solutions for Sumita Arora back exercise assignment. Below includes both textual and video solutions wherever required. View all the answers in assignment for chapter 6 and for all chapters here.

Watch all tutorials for chapter 6.

## Q1: What is a recursive function? Write one advantage of recursive functions.

A recursive function is that which call itself again and again via self-referential expression till condition satisfies.

The major advantage is reduced time complexity and multiple iteration is reduced.

## Q2: What is direct recursion and indirect recursion?

**Direct recursion** occurs when a method calls itself “directly” within its body under the need and condition.

**Indirect recursion **occur when a function is not called by itself but by other function.

For example if a function g calls g again during its cycle then it is direct recursion but whrn function g calls function f during cycle of recursion then it is indirect recursion.

## Q3: What are the two cases required in a recursion function?

The two cases are :

(a) **Base case**

(b) **Recursive case**

## Q4: What is the base case?

**Base case**: The condition under which recursion will occur and till it satisfy the case to be continued.

def factorial(n): if n==1: #part 1 i.e. base case return 0 else: return factorial(n-1)*n

## Q5: What is the recursive case?

Recursive case: Call of recursive function i.e. calling itself.

def factorial(n): if n==1: return 0 else: return factorial(n-1)*n # part2 i.e. recursive case

## Q6: Is it necessary to have a base case in a recursive function? Why/Why not?

Yes, it is highly important as absence of base case will make recursion to run infinitely as the lack of stopping and staring condition.

## Q7: What is infinite recursion? Why does it occur?

Infinite recursion is the case of occurrence of the recursion infinite time.

This may can occur of several reasons:

1. Absence or wrong base case.

2. Not updating the case with each iteration.

3. Improper logic implementation.

## Q8: How can you stop/resolve an infinite recursion?

To stop/resolve an infinite recursion:

1. Make sure presence of base case.

2. Proper logic so that base case must be satisfied at proper time.

## Q9: Give some examples that can be represented recursively.

Some Examples of recursion are:

1. Factorial Number def factorial(n): if n == 0: return 1 else: return n*factorial(n-1) print("factorial of 18 is :", factorial(18))

2. Sum of n number def series_sum(n): if n <1 : return n else: return series_sum(n-1)+n print("Series sum of 100 is :", series_sum(100))

3. Fibonacci Series def fibonacci(n): if n <2 : return 0 elif n==2: return 1 else: return (fibonacci(n-1)+fibonacci(n-2)) print("9th element of fibonacci series is :", fibonacci(9))

## Q10: Can each recursive function be represented through iteration ? Give examples.

Yes, we can easily represent any program with recursion through iteration. The only need will be proper stack and manual calling of function again an again.

# by iteration method def factorial(n): res=1 for i in range(2,len(n)+1): res =res*i return res

# by recursion method def factorial(n): if n==1: return 0 else: return factorial(n-1)*n

## Q11: Which of the functions given in the previous question will result in infinite recursion?

Call by recursive method will result in infinite recursion if n==1 cases is fail or missing.

## Q12: Identify the base case(s) in the following recursive function:

def function1(n):

if n==0:

return 5

elif n==1:

return 8

elif n>0:

return function1(n-1) + function1(n-2)

else:

return -1

In the above case only n>0 is the recursive case and rest is the base cases or terminating case.

## Q13: Why are recursive functions considered slower than their iterative counterparts?

Recursive is slower than the iterative problem as the calling function need extra stack manipulation and in calling the function temporary variable are creates and need extra RAM space and increased time in its cycle.

While in the iterative function no so such expensive routine is required to call the result.

## Q14: If any recursive function can be easily written using iterative code, then what is the need for recursion? When would you prefer recursion over iteration and vice-versa?

Recursive function is required because in cases of larger instances and complexity large number of iterations are required and that easily can be done by the recursive function. Hence, it reduces the complexity at the provided condition.

We must give a push to the recursive approach where the problems are encountered with graphs and trees. As in this situation a stack of own will be created and this will be easily tackled by recursive function. And otherwise with limited iterations we must prefer the iterative function.

## Q15: Compare and contrast the use of iteration and recursion in terms of memory space and speed.

On the contrary of the **speed Iterative **perform **faster **than the recursive function.

On the contrary of the **memory Recursive **uses **more memory** than the iterative function.

Clear Doubts with Computer Tutor

In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to joinCTs learner-teacher community:students.computertutor.in