python recursion practice class 12 Python
python recursion practice class 12
Q 1. what are the two cases required in a recursive function.
ans. the two cases required in a recursive function:-
Q 2. it is necessary to have a base case in a recursive function? why/why not.
ans. yes, it is necessary to have a base case in a recursive function. If there is no base case or the base case is not reachable, it leads to infinite recursion.
Q 3. how can you stop/resolve an infinite recursion.
ans. the infinite recursion is stopped by providing a base case which must get executed, much be reachable for some argument value.
Q 4. out of best-,average-,worst-case complexities, which one gives expected running time of an algorithm.
ans. average case compellability.
Q 5. why are recursive function considered slower than iterative counterparts.
ans. recursive functions are slower as they involve multiple function calls. Additional function-call overheads make them consume more time.
Q 6. what are base case and recursive case? what is their role in a recursive program?
ans. in a recursive solution ,the base cases are predetermined solutions for the simplest versions of the problem: if the given is a base case, no further computation is necessary to get the result.
the recursive case is the one that calls the function again with a new set of values. the recursive step is a set of rules that eventually reduces all versions of the problem to one of the base cases when applied repeatedly.
Q 7. when does infinite recursion occur ?
ans. infinite recursion is when a recursive function executes itself again and again, endlessly. this happens when either the base case is missing or it is not reachable.
Q 8. compare iteration and recursion.
ans. in, iteration, the code is executed repeatedly using the same memory space. that, the memory space allocated once, is used for each pass of the loop.
Q 9. state one advantage and one disadvantage of using recursion over iteration.
ans. advantage:-recursion makes the code short and simple while iteration makes the code longer comparatively.
disadvantage :-recursion is slower than iteration due to overhead of multiple function calls and maintaining a stack for it.
Q 10. figure out the error in the following recursive code of factorial:
ans. the error in above code is that the recursive case is calling function factorial()with a changed value but returning the result of the call.
the correct version will be:-
Q 11. why is the following code printing 1 endlessly.
ans. the above code is printing 1 endlessly because the base case i>n is never reachable. The reason is that value i becomes 1 for each call to function out_upto().
Q 12. write recursive code to complete and print sum of square of n numbers. Value of n is passed as parameter.
ans. def sqsum(n):
return n*n + sqsum(n-1)
n=int(input(“enter value of n:”))
(recursion important questions class 12)
Q 13. write recursive code to compute greatest common divisor of two numbers.
ans. def gcd(a,b):
n1=int(input(“enter first number:”))
n2=int(input(“enter second number:”))
Q 14. program to show the use of recursion in calculation of power eg, ab.
ans. def power(a,b):
print(“enter only the positive numbers below”)
num=int(input(“enter base number:”))
p=int(input(“raised to the power of:”))
print(num,”raised to the power of”,p,”is”,result)
Q 15. recursive function to print string in reverse order.
ans. def reverse(s):
return(s[-1] + reverse(s[0:len(s)-1))