50% found this document useful (2 votes)
5K views

Working With Functions Xii Output Questions

1. The document contains examples of functions and questions related to functions in Python. 2. It demonstrates functions with parameters, default arguments, global and local variables, error handling, and positional vs keyword arguments. 3. Sample code includes functions to calculate areas, sums, swapping variables, and applying discounts. Questions test understanding of parameter passing, default values, operator usage, and syntax errors.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
5K views

Working With Functions Xii Output Questions

1. The document contains examples of functions and questions related to functions in Python. 2. It demonstrates functions with parameters, default arguments, global and local variables, error handling, and positional vs keyword arguments. 3. Sample code includes functions to calculate areas, sums, swapping variables, and applying discounts. Questions test understanding of parameter passing, default values, operator usage, and syntax errors.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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.

4.def fun1(x, y):


x=x+y
y=x–y
x=x–y
print(‘a=’,x)
print(‘b=’,y)
a=5
b=3
fun1(a,b)
Ans.: a= 3, b= 5
Explanation: a = 5 and b = 3 passed into function. When cursor jumps to function
header fun1(x,y) is seems like fun1(5,3). Then
x = x + y i.e. x = 5 + 3 = 8
y = x – y i.e. y = 8 – 3 = 5
x = x – y i.e. x = 8 – 5 = 3
So finally a 3 and b = 5.
5. def div5(n):
if n%5==0:
return n*5
else:
return n+5
def output(m=5):
for i in range(0,m):
print(div5(i),’@’,end=” “)
print(‘n’)
output(7)
output()
output(3)
Ans.:
0 @ 6 @ 7 @ 8 @ 9 @ 25 @ 11 @
0@6@7@8@9@
0@6@7@
Explanation:
The function output(7)  in this case m=7
Now, the range starts with 0 to 6.
When i = 0, div5(i), 0 % 5 = 0  n * 5  0 * 5  0, Now jump to
loop  print  o @
When i = 1, div5(i), 1 % 5 = 1  n + 5  1 + 5  6, Now jump to
loop  print  6 @
When i = 2, div5(i), 2 % 5 = 2  n + 5  2 + 5  7, Now jump to
loop  print  7 @
When i = 3, div5(i) 3 % 5 = 3  n + 5  3 + 5  8, Now jump to
loop  print  8 @
When i = 4, div5(i) 4 % 5 = 4  n + 5  4 + 5  9, Now jump to
loop  print  9 @
When i = 5, div5(i) 5 % 5 = 0  n + 5  5 * 5  25, Now jump to
loop  print  25 @
When i = 6, div5(i) 6 % 5 = 1  n + 5  6 + 5  25, Now jump to
loop  print  11 @
So line 1 is 0 @ 6 @ 7 @ 8 @ 9 @ 25 @ 11 @
Similarly without parameter m=5, Do your self.
6.def sum(*n):
total=0
for i in n:
total+=i
print(‘Sum=’, total)
sum()
sum(5)
sum(10,20,30)
Ans.:
Sum= 5
Sum= 10
Sum= 30
Sum= 60
Explanation:
Function 1: sum() without argument so output is None.
Function 2: sum(5)  i = 5  total + = i  total = total + i  0 + 5  5
Function 3: sum(10,20,30)  n(10,20,30)  i = 10  total + i = 0 +
10  10
sum(10,20,30)  n(10,20,30)  i = 20  total + i = 10 +
20  30
sum(10,20,30)  n(10,20,30)  i = 30  total + i = 30 +
30  60
The next question is based on use of Global key word 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=100temp = 10 + 100 = 110x = 110 +
10120120!=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=10temp = 20 + 1030 x = 30 +
20  50  50!=20050
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

3. // Program to compute result


def res():
eng = 56
math = 40
sci = 60
if eng<=35 || math<=35 ||
sci=35
print(‘Not Qualified’)
else:
print(“Qualified”)
Ans.: // Program to compute result  // is not used in python, it should be
#
if eng<=35 || math<=35 ||  || is not any operator in python, it
should be or
sci=35  It should written in above line or should be use in upper line
This question is based on positional argument for working with functions xii.

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

5. def cal_dis(qty,rate=50,dis_rate): #discount rate = 5%


bil_amt = (qty*rate)*dis_rate
print(bil_amt)
caldis(10)
cal_dis(10,50,0.05)
Ans.: def cal_dis(qty,rate=50,dis_rate=0.05): #discount rate = 5%
caldis(10)  Function call statement; cal_dis should be there

You might also like