Working With Function || Type B || Sumita Arora || Class 12 || Computer science || Information practices || Solution ||



Q 1 = What are the errors in following codes? Correct the code and predict output:

(a)

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


(b)

def Tot (Number) #Method to find Total
    Sum = 0
    for C in Range (1, Number + 1):
    Sum += C
    RETURN Sum
print (Tot[3]) #Function Calls
print (Tot[6])




Q 2 = Consider the following code and write the flow of execution for this. Line numbers have been given for your reference.

def power (b, p):        1
    y = b ** p        2
    return y        3
            4
def calcSquare(x):        5
    a = power (x, 2)        6
    return a        7
            8
n = 5            9
result = calcSquare(n)    10
print (result)        11








Q 3 = What will the following function return?

def addEm(x, y, z):
    print (x + y + z)




Q 4 = What will the following function print when called?

def addEm(x, y, z):
    return x + y + z
    print (x+ y + z)





Q 5 = What will be the output of following programs?

(i)
num = 1
def myfunc ():
    return num
print(num)
print(myFunc())
print(num)

(ii)
num = 1
def myfunc():
    num = 10
    return num
print (num)
print(myfunc())
print(num)

(iii)
num = 1
def myfunc ():
    global num
    num = 10
    return num
print (num)
print(myfunc())
print(num)

(iv)
def display():
    print("Hello", end = ' ')
display()
print("there!")





Q 6 = 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)





Q 7 = What is wrong with the following function definition?

def addEm(x, y, z):
    return x + y + z
    print("the answer is", x + y + z)





Q 8 = Write a function namely fun that takes no parameters and always returns None.





Q 9 = 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?






Q 10 = Consider the code below and answer the questions that follow:

def multiply(number1, number2):
    answer = number1 * number2
    return(answer)
print(number, 'times', number2, '=', answer)
output = multiply(5,5)

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





Q 11 = Find the errors in code given below:

(a)

def minus (total, decrement)
    output = total - decrement
    print(output)
    return (output)

(b)

define check()
    N = input ("Enter N: ")
    i = 3
    answer = 1 + i ** 4 / N
    Return answer

(c)

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

def beta (string)
    return string == str(n)

print(alpha("Valentine's Day") :)
print(beta (string = 'true' ))
print(alpha(n = 5, "Good-bye") :)





Q 12 = Draw the entire environment, including all user-defined variables at the time line 10 is being executed.


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





Q 13 = Draw flow of execution for above program.





Q 14 = 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





Q 15 = 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





Q 16 = Write a program with non-void version of above function and then write flow of execution for both the programs.





Q 17 = What is the output of 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]
mi, m2, m3, m4 = increment (L)
print(L)
print(mi, m2, m3, m4)
print(L[3] == m4)




7 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post