[Type B] Chapter– 6 Class 12 CS – Sumita Arora Assignment | Q/A

Here is class 12 computer science Unit 6 [Type B] 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: Compute square numbers defined as follows:
Chapter 6 Type-B Sumita Arora
Question 1
Solution:
(c) compute(3) = compute(2) + 2*3-1
Q2: Look at the numbers again:
Chapter 6 Type-B Sumita Arora
Question 2

Solution:

(b)
def compute(N):
    if N == 1:
        return 1
    else:
       return compute(N-1)+2*N-1
Q3: Look at compute numbers one more time:

compute(1) = 1
compute(N) = compute(N-1) + 2N-1
How many invocations will there be on the call of the compute(5)?

(a) 1
(b) 3
(c) 5
(d) 6
(D) 6
Q4: Predict the output of the following code:
Chapter 6 Type-B Sumita Arora
Question 4
Chapter 6 Type-B Sumita Arora
Question 4

Solution:

(a)
15
12
9
6
3
Finally

(b)
Infinite loop

(c)
10
8
6
4
2
Finally

(d)
Infinite loop
Q5: Predict the output of the following codes:
def express(x, n):
    if n == 0:
        return 1
    elif n%2 == 0:
        return express(xx, n/2) 
    else: 
        return express(x, n-1)

express(2,5)

Solution:

Nothing will be displayed on the output screen as absence of print() function.
Hence, no output.

Note: But beside this fact 32 will be finally result of the above code as the express() is called.
Q6: Consider the 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)?

Solution:

True
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.
Yes, the last condition will make an infinite recursion to occur. If any number odd and greater than 1 is passed than the code will enter in infinite recursion.

Hence, in the case of check(3) an infinite loop will occur.
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)

Solution:

Output:
(a) 12 6 3 1 1 2 4 2 1 -3
(b) 10 5 2 1 3 1 1 1
(c) 7 3 1 1 2 -2
Q9: Figure out the problem with the following code that may occur when it is run?
def recur(p):
  if p==0:
    print("##")
  else:
    recur(p)
    p=p-1
recur(5)

Solution:

This code will act as infinite recursion as the p=p-1 will never going to run as of positioning.
Q10: Check Point 6.1 has some recursive functions that cause infinite recursion. Make changes in the definitions of those recursive functions so that they recur finitely and produce a proper output.
#the corrected code will be
def recur(p):
  if p==0:
    print("##")
  else:
    p=p-1
    recur(p)

recur(5)

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 join CTs learner-teacher community: students.computertutor.in

You cannot copy content of this page