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

Here is class 12 computer science Unit 3 [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 3 and for all chapters here.

Watch all tutorials for chapter 3: Python Revision Tour.

Q1: What are the errors in the following codes? Correct the code and predict output:
Chapter 3 Type-B Sumita Arora
Question 1

Solution:

Indentation and invalid keyword selection that means syntax error is present in the both cases.

(a) Corrected Code
total =0
def sum(arg1, arg2):
    total = arg1+arg2
    print("Total :",total)
    return total
sum(10,20)
print("Total :",total)

Output:
Total : 30
Total : 0

(b) Corrected Code
def Tot(Number):
    Sum = 0
    for C in range(1, Number+1):
        Sum+=C
    return Sum
print(Tot(3))
print(Tot(6))

Output:
6
21
Q2: Consider the following code and write the flow of execution for this. Line numbers have been given for your reference.
Chapter 3 Type-B Sumita Arora
Chapter 2

Solution:

Firstly the line 9 will execute and a variable with value 5 will be initialized.
After that in line 10 calcSquare(n) will be called and the respective function will execute means Line 5 will be called.
Then on moving line 6 power function will call and the flow jump to line 1 and result of power function stored in variable a.
Then flow will jump to the line 6 and then 7 and then to line 10 to initialize the result variable.
Then, the Line 11 will be executed to provide output.

Flow cycle is 
9 → 10 → 5 → 6 → 1 → 2 → 3 → 7 → 10 → 11
Q3: What will the following function return?

def addEm(x, y, z):
    print(x+y+z)
Solution : This function will return nothing as of no return keyword.
Q4: What will the following function print when called?


def addEm(x, y, z):
    return (x+y+z)
    print(x+y+z)
Solution:
This function will print depend on case:
If x, y, z will be numeric value than a numeric value will be printed.
If string will be passed as argument than a string concatenation will be result.
Q5: What will be the output of the following programs?
Chapter 3 Type-B Sumita Arora
Question 5

Solution:

(a) 
1
1
1

(b)
1
10
1

(c)
1
10
10

(d)
Hello there!
Q6: Predict the output of the following code:

a =10
y = 5
def myfunc():
    y=a
    a=2
    print("y=", y, "a=", a)
    print("a+y=", a+y)
    return a+y
print("y=", y, "a=", a)
print(myfunc())
print("y=", y, "a=", a)

Output:
y= 5 a= 10
UnboundLocalError: local variable 'a' referenced before assignment

Note: The unbound error occur because during call of myfunc() local variable is used before initializing the same in local scope.
Q7: What is wrong with the following function definition?

def addEm(x,y,z):
    return x+y+z
    print("the answer is", x+y+z)
The print command is initialized after return command and return command is termination of any function call. Hence, the print() command will never run in such call.
Q8: Write a function namely fun that takes no parameters and always returns None.
def fun():
    return None
Q9: Consider the code below and answer the questions that follow:

def multiply(number1, number2):
    answer = number1*number2
    print(number1, 'times', number2, "=", answer)

    return(answer)

output = multiply(5,5)

(i) When the code above is executed, what prints out?
(ii) What is variable output equal to after the code is executed?
(i) Output:
5 times 5 = 25
(ii)  output = 25
Q10: Consider the code below and answer the questions that follow:

def multiply(number1, number2):
    answer = number1*number2
    return(answer)
    print(number1, 'times', number2, "=", answer)

output = multiply(5,5)

(i) When the code above is executed, what prints out?
(ii) What is variable output equal to after the code is executed?

(i) Output:
Nothing
(ii) output = 25
Q11: Find the errors in the code given below:
Chapter 3 Type-B Sumita Arora
Question 11
Chapter 3 Type-B Sumita Arora

Solution:

(a)Correct code:
def minus(total, decrement):    # semicolon was not given
    output = total-decrement
    print(output)
    return output

(b) Correct code:
def check():      # define → def  and semicolon absent
    N =input('Enter N:')
    i =3
    answer =1+i**4/N
    return answer    # Return  → return

(c) Correct code

def alpha(n, string='xyz', k=10):
    return beta(string)
    return n

def beta(string): #  Semicolon absent
    return string == str(n)

print(alpha("Valentine Day"))  # wrong semicolon present
print(beta(string='true'))
print(alpha(n=5, "Good bye"))  # wrong semicolon present
Q12: Draw the entire environment, including all user-defined variables at timeline 10 is being executed:

1. def sum(a, b, c, d):
2.     result = 0
3.     result =result+a+b+c+d
4.     return result
5.
6. def length():
7.     return 4
8.
9. def mean(a, b, c, d):
10.    return float(sum(a, b, c, d))/length()
11.
12. print(sum(a, b, c, d), length(), mean(a, b, c, d))

Solution:

Till line 10 is called means a local environment being created and all the three function sum(), length(), mean() being called as by line 12 respectively. 

Which shows that from the sum() variable a, b, c, d will globally access by function and acted upon local variable result and returned.

In case of length() no variable acceptance and declaration will be done. 

In case of mean() variable a, b, c, d will globally access by function and calculated portion will return.
Q13: Draw the flow of execution for the above program.
12 → 1 → 2 → 3 → 4 → 12 → 6 → 7 → 12 → 9 → 10

Q14: In the following code, which variables are in the same scope?

def func1():
    a=1
    b=2

def func2():
    c=3
    d=4

e=5
Variable a and b having same local scope for function func1().
Variable c and d having the same local scope for function func2().
Q15: Write a program with a function that takes an integer and prints the number that follows after it. Call the function with these arguments:

4, 6, 8, 2+1, 4-3*2, -3-2
def follow_num(n):
    return n+1

follow_num(4)
follow_num(6)
follow_num(8)
follow_num(2+1)
follow_num(4-3*2)
follow_num(-3-2)

Output:
5
7
9
4
-1
-4

Q16: Write a program with a non-void version of the above function and then write flow of execution for both the programs.
# code
1. def follow_num(n):
2.     return n+1

#non-void code
3.  def follow_num_non(n): 
4.      print(n+1)
5.      return n+1
6.
7.  follow_num(5)
8.  follow_num_non(4)

Flow control
7 → 1 → 2 → 8 → 3 → 4 → 5 
Q17: What is the output of the following code fragments?
(i)
def increment(n):
    n.append([4])
    return n
L = [1,2,3]
M =increment(L)
print(L,M)


(ii)
def increment(n):
    n.append([49])
    return n[0], n[1], n[2], n[3]
L = [23,35,47]
m1, m2, m3, m4 =increment(L)
print(L)
print(m1, m2, m3, m4)
print(L[3] == m4)

output:
(i)  
[1, 2, 3, [4]] [1, 2, 3, [4]]

(ii) 
[23, 35, 47, [49]]
23 35 47 [49]
True

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