Sir Mutha School
Computer Science Worksheet
Lesson – Working with Functions
1) Predict the output
def my_func(var1=100, var2=200):
var1+=10
var2 = var2 - 10
return var1+var2
print(my_func(50),my_func())
2) Predict the output
def c(x,y):
if x%y:
return x+5
else:
return y+10
print(c(20,5))
3) Predict the output
def add (num1, num2):
sum = num1 + num2
sum = add(20,30)
print(sum)
4. Find the errors in following function definitions :
a) def main()
print ("hello")
b) square (a)
return a * a
5. Which of the following items are present in the function header ?
a) function name only b) both function name and parameter list
c) parameter list only d) return value
6.Which of the following function headers is correct ?
a) def f(a = 1, b):
b) def f(a = 1, b, c = 2):
c) def f(a = 1, b = 1, c = 2):
d) def f(a = 1, b = 1, c = 2, d):
7. Which of the following statements is not true for parameter passing to functions ?
a) You can pass positional arguments in any order.
b) You can pass keyword arguments in any order.
c) You can call a function with positional and keyword arguments.
d) Positional arguments must be before keyword arguments in a function call.
8. Which of the following is not correct in context of scope of variables ?
a) Global keyword is used to change value of a global variable in a local scope.
b) Local keyword is used to change value of a local variable in a global scope.
c) Global variables can be accessed without using the global keyword in a local scope.
d) Local variables cannot be used outside its scope.
9. Which of the following function calls can be used to invoke the below function definition ?
def test(a, b, c, d)
a) test(1, 2, 3, 4)
b) test(a = 1, 2, 3, 4)
c) test(a = 1, b = 2, c = 3, 4)
d) test(a = 1, b = 2, c = 3, d = 4)
10. For a function header as follows :
def Calc(X,Y = 20):
Which of the following function calls will give an error ?
a) Calc(15, 25)
b) Calc(X = 15, Y = 25)
c) Calc(Y = 25)
d) Calc(X = 25)
11. What is the result of this code ?
def print_double(x):
print(2 ** x)
print_double(3)
12.
Assertion. A function is a subprogram.
Reason. A function exists within a program and works within it when called.
13.
Assertion. A parameter having a default in function header becomes optional in function
call.
Reason. A function call may or may not have values for default arguments.
14. Write the term suitable for following descriptions:
(a) A name inside the parentheses of a function header that can receive a value.
(b) An argument passed to a specific parameter using the parameter name.
(c) A value passed to a function parameter.
(d) A value assigned to a parameter name in the function header.
(e) A value assigned to a parameter name in the function call.
(f) A name defined outside all function definitions.
(g) A variable created inside a function body.
15. What are the errors in following codes? Correct the code and predict output:
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])
16. Consider the following code and write the flow of execution for this. Line numbers have
been given for your reference.
1. def power(b, p):
2. y = b ** p
3. return y
4.
5. def calcSquare(x):
6. a = power(x, 2)
7. return a
8.
9. n = 5
10. result = calcSquare(n)
11. print(result)
17. What is wrong with the following function definition ?
def addEm(x, y, z):
return x + y + z
print("the answer is", x + y + z)
18. Write a function that takes two numbers and returns the number that has minimum
one's digit.
19. Write a function that receives two string arguments and checks whether they are same-
length strings (returns True in this case otherwise False).
20. Predict the output.
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
21. Predict the output
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)
22. Predict the output
def add(i):
if(i*3%2==0):
i*=i
else:
i*=4
return i
a=add(10)
print(a)
b=add(5)
print(b)
23. What will be the output of following code?
def display(s):
l = len(s)
m=""
for i in range(0,l):
if s[i].isupper():
m=m+s[i].lower()
elif s[i].isalpha():
m=m+s[i].upper()
elif s[i].isdigit():
m=m+"$"
else:m=m+"*"
print(m)
display("[email protected]"
24. Give the difference between local scope and global scope.
25. What will be the output of following code?
def display(s):
l = len(s)
m=""
for i in range(0,l):
if s[i].isupper():
m=m+s[i].lower()
elif s[i].isalpha():
m=m+s[i].upper()
elif s[i].isdigit():
m=m+"$"
else:
m=m+"*"
print(m)
display(“[email protected]”)