Recursion || Type B || Sumita Arora || Class 12 || Computer science || Information practices || Solution ||






Q2 = Look at compute numbers again:

compute (1) = 1
compute (N) = compute (N-1) + 2N-1

Which Python function below successfully implements this definition?

(a)
def compute(N):
    if N<1:
        return 1
    else:
        return N. N

(b)
def compute(N) :
    if N ==1:
        return 1
    else:
        return compute(N-1) + 2 *N – 1

(c)
def compute(N) :
    if N = 1:
        return 1
    else:
        return compute(N-1) + 2 *N – 1

(d)
def compute(N) :
    if N = 1:
        return 1
    else:
        return compute(N)





Q3 = Look at compute numbers one more time:

compute(1) = 1
compute (N) = compute(N-1) + 2N-1

Assume the definition has been implemented correctly. How many invocations will there be on the call stack is _main_ is compute (5)?

(A) 1
(B) 3
(C) 5
(D) 6






Q4 = Predict the output of following codes.

(a)
def codo (n):
    if n== 0:
        print('Finally ')
    else:
              print(n)
              codo(n - 3)
codo(15)

(b)
def codo (n):
    if n== 0:
        print('Finally ')
    else:
              print(n)
              codo(n - 2)
codo(15)

(c)
def codo (n):
    if n== 0:
        print('Finally')
    else:
              print(n)
              codo(n - 2)
codo(10)

(d)
def codo (n):
    if n== 0:
        print('Finally')
    else:
              print(n)
              codo(n - 3)
codo(10)





Q5 = Predict the output of following code.

def express(x, n):
    if n== 0:
        return 1
    elif n%2 == 0:
        return express(x*x, n/2))
    else:
        return x * express (x, n-1)
express (2,5)





Q6 = Consider following Python function that uses recursion:

def check(n):
    if n <= 1:
        return True
    elif n % 2 == 0 :
        return check(n/2)
    else:
        return check(n/1)

What is the value returned by check(8)?





Q7 = Can you find an error or problem with the above code? Think about what happens if we evaluate check(3). What output would be produced by Python? Explain the reason(s) behind the output.





Q8 = Consider the following Python function Fn(), that takes an integer n parameter and works recursively as follows:

def Fn(n):
    print(n, end=" ")
    if n< 3:
        return n
    else:
        return Fn(n // 2) - Fn(n//3)

What will be the result produced by following function calls?

(a) Fn(12)
(b) Fn(10)
(c) Fn(7)





Q9 = Figure out the problem with following code that may occur when it is run?

def recur(p) :
    if p == 0:
        print("##")
    else:
        recur(p)
        p=p-1
recur(5)



Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post