Question – 7: Predict the output of the following code :
Answer: Output:-
y = 5 a = 10
y = 10 a = 2
a + y = 12
y = 5 a = 10
Question – 8: What is wrong with the following function definition?
def addEm(x, y, z): return x + y + z print("the answer is", x + y + z)
Answer: The control can not reach the print( ) statement. It should be written before return statement.
def addEm(x, y, z): print("the answer is", x + y + z) return x + y + z
Question – 9: Create a Function that takes no parameter and returns None.
Answer: –
def fun(): return None
Question – 10: Consider the code given (for code refer to book) and answer the questions –
(i) When the code is above executed, what prints out?
Answer: Output:- 5 times 5 = 25
(ii) What is the variable output equal to after the code is executed?
Answer: The value of variable output is 25
Question – 11: Same as Q 10
Answer: –
Question – 12: Find the errors [for code refer to book]-
(a)
Answer: – (a) Missing of colon , in function header. It should be –
def minus (total, decrement ) :
(b) and (c)
Answer: – (b) Error i -> In place of define write def in function header. Error ii-> Missing of colon , in function header. Error iii -> N is string can not be used in arithmetic expression. Error iv -> Return should be return.
Corrected code is:-
def check( ): # Error (i) & (ii) N = input('Enter N :') i = 3 answer = 1 + i ** 4 / int(N) #Error (iii) return answer #Error (iv)
(c)
Answer: – (c) Error i -> In function alpha, return statement used two times. It should be one time.
Error ii -> In function beta, Name Error – name ‘n’ is not defined.
Error iii -> Invalid use of : inside the print() .
Error iv -> Invalid use of : inside the print() .
Question – 13: Draw the environment variable
Answer: uploading soon
Question – 14: Draw flow of execution of above code
Answer: 12 -> 1 -> 2 -> 3 -> 4 -> 12 -> 6 -> 7 -> 12 -> 9 -> 10 -> 1 -> 2 -> 3 -> 4 -> 10 -> 6 -> 7 -> 10 -> 12
Question – 15: In the given code, which variables are in the same scope?
Answer: a & b, and c & d
Question – 16: 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
Answer:
def printfollow(n):
print(n+1)
printfollow(4)
printfollow(6)
printfollow(8)
printfollow(2+1)
printfollow(4-3*2)
printfollow(-3-2)
Output:
5
7
9
4
-1
-4
Question – 17: Write a program with non-void version of above function and then write the flow of execution for both the programs.
Answer:
def printfollow(n):
retutn (n+1)
print(printfollow(4))
print(printfollow(6))
print(printfollow(8))
print(printfollow(2+1))
print(printfollow(4-3*2))
print(printfollow(-3-2))
Output
5
7
9
4
-1
-4
Flow of control of both program Q-16 & Q-17 are :
3->1->2->4->1->2->51->2->6->1->2->7->1->2->8->1->2->End of program
Question – 18: What is the output of following code fragments
(a)
def increment(n):
n.append([4])
return n
L = [1, 2, 3]
M = increment(L)
print(L, M)
Answer: Output:
[1, 2, 3, [4]] [1, 2, 3, [4]]
Question – 18: What is the output of following code fragments
(b)
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)
Answer: Output:
[23, 35, 47, [49]]
23 35 47 [49]
True