Working With Functions Xii Output Questions
Working With Functions Xii Output Questions
1. def Fun1():
print(‘Python, let’s fun with functions’)
Fun1()
Ans.: Python, let’s fun with functions.
Explanation: The code has a simple print function. is used to print character ‘
in python language.
2. def add(i):
if(i*3%2==0):
i*=i
else:
i*=4
return i
a=add(10)
print(a)
Ans. : 100
Explanation: A add() passed one variable i.e a=10.
So i=10
10 * 3 % 2 =0 means first priority will be given to * operator
30 % 2 = 0, Condition is True as remainder will be 0
i*=i i.e. i=10*10=100.
3. import math
def area(r):
return math.pi*r*r
a=int(area(10))
print(a)
Ans.: 314
Explanation: Function area() has one parameter r, and passed value is 10. When it
execute the return statement of function, it calculates value 3.14*10*10
(pie= 3.141592653589793), so the answer will be 314.1592653589793. Now in
call statement int() function is used that converts answer into interger.
The next question is based on parameters for working with functions xii.
6.def func(b):
global x
print(‘Global x=’, x)
y=x+b
x=7
z=x–b
print(‘Local x = ‘,x)
print(‘y = ‘,y)
print(‘z = ‘,z)
x=5
func(10)
Ans: x is used as global and local variable.
Global x= 5
Local x = 7
y = 15
z = -3
Explanation:
Values of variables: x global 5 , b passed as an argument 10
y = x + b = 5 + 10 15
x=7
z = 7 – 10 = -3
This questions is based on default argument for working with functions xii.
7. def func(x,y=100):
temp = x + y
x += temp
if(y!=200):
print(temp,x,x)
a=20
b=10
func(b)
print(a,b)
func(a,b)
print(a,b)
Ans.:
110 120 120
20 10
30 50 50
20 10
Explanation:
Function 1:
func(b)x=b=10, y=100temp = 10 + 100 = 110x = 110 +
10120120!=200 120
So Line 1, print(temp,x,x) temp=110, x 120 110 120 120
Line 2 print(a,b) 20 10
Function 2:
Func(a,b) x=20, y=10temp = 20 + 1030 x = 30 +
20 50 50!=20050
Line 3 print(temp,x,x) temp = 30, x= 50 30 50 50
Line 4 print(a,b) 20 10
8. def get(x,y,z):
x+=y
y-=1
z*=(x-y)
print(x,’#’,y,’#’,z)
def put(z,y,x):
x*=y
y+=1
z*=(x+y)
print(x,’$’,y,’$’,z)
a=10
b=20
c=5
put(a,c,b)
get(b,c,a)
put(a,b,c)
get(a,c,b)
Ans.:
100 $ 6 $ 1060
25 # 4 # 210
100 $ 21 $ 1210
15 # 4 # 220
Explanation:
Function 1:put(a,c,b)a=z=10, c=y=5, b=x=20
x= 5 x 20 100 y= 5 + 1 = 6 z = 10 * (100 +6) 1060
Line 1 print(x,’$’,y,’$’,z) 100 $ 6 $ 1060
Function 2:get(b,c,a)b=x=20, c=y=5, z=a=10
x = 20 + 5 = 25 y = 5 – 1 = 4 z = 10 * (25-4) 10 * 21 210
Line 1 print(x,’$’,y,’$’,z) 25 # 4 # 210
Error-based questions
1. def in(x,y):
x=x + y
print(x.y)
x*=y
print(x**y)
Ans.: def in(x,y): in can’t be used as function name because it’s a
keyword
print(x.y) The variable in python print() function separate by
comma not dot
The next questions is based on default argument for working with functions xii.
2. void get(x=10,y):
x=x+y
print(x,n,y)
Ans.: void get(x=10,y): Default argument must be assign a value from
right to left
print(x,n,y) ‘n’ must be enclosed with quotes
4. a=5, b=10
def swap(x,y):
x=a+b
y=x–y
x=x–y
swap(a)
swap(15,34)
swap(b)
swap(a,b)
Ans.: a=5, b=10 In python variable should not assign in single line by comma
swap(a) The positional arguments must be passed
swap(b) Same as above