Find the solved host questions for working with functions xii output and error based 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.

 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

Similarly function 3 & function 4 will execute, do in practice.

In the next section of working with functions xii you will get error-based questions. 

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

So here I have tried with the most common topics from working with functions xii with questions and answers.

For all the contents of computer science click on the below given link:

Computer Science Class XII

One thought on “Working with Functions XII – 13 Output & Error Questions with easy solutions”

Leave a Reply