Function Exercise

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Function Exercise

1) Which of the following items are present in function header?


a) Function name only.
b) Both function name and parameter list.
c) Parameter list only.
d) Return value.

2) Which of the following keyword marks the beginning of the function block?
a) func b) define c) def d) function

3) What is the name given to that area of memory, where the system stores the parameters
and local variable of a function call?
a) heap b) storage area c) stack d) array

4) Which of the following function headers is correct?


a) def(a=1,b):
b) def(a=1,b,c=2):
c) def(a=1,b=1,c=2):
d) def(a=1,b=1,c=2,d):

5) Which of the following function call can be used to invoke the following function header?
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)

6) What is a variable defined outside all the functions referred to as?


a) Built in variable
b) Global variable
c) Local variable
d) Automatic variable
7) Carefully observe the code and tell the output of this:
def function(a):
a=a+’1’
a=a*2
function(‘hello’)
a) error
b) hello1hello1
c) hello12
d) no output

8) 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 keyword arguments in a function call.

9) What is the order of resolving scope of a name in a python program.


[L:Local, G: Global, E:Enclosing, B:Built-in]
a) BGEL
b) LEGB
c) LGEB
d) LBEG

10) Which of the given argument types can be skipped from a function call?
a) Positional arguments
b) Keyword arguments
c) Named arguments
d) Default arguments

11) Predict the output of the following:


def test(x=1,y=2):
x=x+y
y+=1
print(x,y)
print(test())
a) 3,3 b) 3,4 c) 4,5 d) 2,3
12) Values being passed are called _______ and values being received are called
_______
a) Formal arguments, Actual arguments
b) Actual arguments, Formal arguments
c) Local variables, Global variables
d) Global variables, Local Variables

13) Arguments appear in function header statement are ______


a) Actual arguments
b) Formal arguments
c) Local variables
d) Global variables

14) What will be the output of the following Python code?


def add (num1, num2):
sum = num1 + num2
sum = add(20,30)
print(sum)

a. 50
b. 0
c. Null
d. None

15) What will be the output of the following code?


def my_func(var1=100, var2=200):
var1+=10
var2 = var2 - 10
return var1+var2
print(my_func(50),my_func())

a. 100 200
b. 150 300
c. 250 75
d. 250 300
16) def test():
global a
a+=10
print(a)
a=90
print(a)
test()
print(a)

i) 90 100 90 ii) 90 100 100 iii) 100 100 100 iv) Error

17) def test():


a=20
print(a)
a=90
print(a)
test()
print(a)
i) 90 20 20 ii) 90 20 90 iii) 20 20 90 iv) Error

18) x=50
def func():
global x
print(x)
x=2
print(x)
func()
print(x)
i) 50 2 2 ii) 50 2 50 iii) 50 50 2 iv) Error
19) def area(l,b):
return l*b
def peri(l,b):
return 2*(l+b)
a=area(7,3)
p=peri(7,3)
print(a,p)

i) 21 20 ii) 20 21 iii) 20 20 iv) 21 21


20) A function
(a) can’t return any value
(b) can return only 1 value
(c) can return 1 or 2 value
(d) can return many values

21) Which of the following function header is incorrect?


(a) def greater(a,b)
(b) def greater(a=5,b)
(c) def greater(a=5,b=10)
(d) def greater(a,b=10)

22) print(“hi”)
def abc():
print(“hello”)
print(“bye”)
abc()
a) hi
hello
bye
b) hi
bye
hello
c) hello
bye
hi
d) bye
hello
hi

23) Which of the following is the incorrect way to call a function?


(a) func(15,20) (b) func(a,b) (c) func() (d) func(15,b=20)
24) Give the output
def abc(a=2,b=4):
return a+b
x=10
y=20
x=abc(x,y)
print(x,y)
(a) 30,20 (b) 6,20 (c) 30,4 (d) 6,4

25) What will be the output of the following code?


a=3
def demo(x,y):
global a,b
a=a+x+y
z=a+y
b+=x
b=5
demo(a,b)
print(a,b)

a) 11,8 b) 11,16 c)11,6 d)11,10

26) Read the following Python code carefully and point out the global variables?
a, b = 25,12
def test():
global i
i = a+b
(a) i
(b) a and b
(c) i, a and b
(d) None of the above.
27) What is the output of the following Python code?
a = 20
def test(a):
print(a)
a=5
test(a)
print(a)
a) 20
20
b) 20
5
C) 20
40
d) Error

28) What is the output of following code?


def work (a=10, b=20):
s=a+b
print(s)
return
x=23
y=62
work(x, y)
a) 30 b) 85 c) 33 d) 82

29) ……… is a variable used to define a particular value during a function definition.
(a) Argument (b) Parameter (c) Both (a) and (b) (d) None of these

30) What is the output of the following Python code?


def test():
return val +1
val= 0
print(test())

(a) 0 (b) 1 (c) Error (d) None


31) Which of the following statement is false?
(a) Global variables are declared outside of all functions.
(b) Global variables are useful for all functions.
(c) Global variables are also called global scope.
(d) Global variables are used as global namespace.

32) What will be the output of the following Python code?


def test():
a=12
print(a−1)
a=+1
test()
(a) 13 (b) 12 (c) 11 (d) Error

33) What will be the output of following code?


def Func(x):
print(x+2)
x=4
x=−5
Func(37)
(a) 37 (b) 41 (c) 39 (d) 32

34) What is the output of following code?


a=5
def MyFunc() :
global a
a =a + 3
MyFunc()
print(a)
(a) 3 (b) 8 (c) 5 (d) Error
35) What will be the output of the following Python code?
def myFunc(a, b):
if a > b :
return a
elif a = = b:
return ‘Equal numbers’
else:
return b
print(myFunc(2, 8))
(a) 8 (b) 2 (c) Equal numbers (d) Error

36) Function blocks begin with the _______________ .


(a) fun (b) define (c) def (d) open

37) Which of the following function header is incorrect?


(a) def sum(a,b,c)
(b) def sum(a,b,c=10)
(c) def sum(a,b=5,c=10)
(d) def sum(a=4,b,c=10)

38) def sum(a,b=10):


print(a+b)
sum(15,25)
sum(15)
(a) 40 25 (b) 25 25 (c) 35 15 (d) 40 10
39) def fun():
a=10
print(a,end="#")
a=5
fun()
print(a)
(a) 10#5 (b) 10#10 (c) 5#5 (d) 5#10

40)Which of the following is the correct way to call a function?


a. my_func()
b. def my_func()
c. return my_func
d. call my_func()
41) value = 50
def display(N):
global value
value = 25
if N%7==0:
value = value + N
else:
value = value - N
print(value, end="#")
display(20)
print(value)
a. 50#50
b. 50#5
c. 50#30
d. 5#50#

You might also like