Assignment -1 : User-defined functions
1 Observe the following Python code and select the correct option from the given
options:
a= 20
def convert(a):
b = 20
a= a+b
convert(10)
print (a)
a. 10
b. 20
c. 30
d. Error
2 Predict the output:
def callon( b=20, a=10):
b=b+a
a= b-a
print (b,”#”,a)
return b
x=100
Y=200
x= callon(x,y)
print (x,”@”,y)
y=callon(y)
print (x,”@”,y)
3 What is the purpose of the global keyword in Python?
a. To define a variable inside a function
b. To access a variable outside a function
c. To modify a variable defined in the global scope from within a function
d. To specify a variable as constant
4 def countdown(n):
if n <=0:
print (“done!”)
else:
print (n)
countdown(n-1)
countdown(3)
a. 3 2 1 Done!
b. 1 2 3 Done!
c. Done! 1 2 3
d. Done!
4 Which of the following is true about variable scope in Python?
a. Local variables can be accessed outside the function in which they are
defined
b. Global variables take precedence over local variables
c. Variables defined inside a function have global scope
d. Variables defined inside a function have a local scope
5 def add(a,b):
return a+b
def subtract (a,b):
return a-b
operations = {“add”: add,” subtract”: subtract}
result1= operations[“add”](5,3)
result2 = operations[“subtract”](7,2)
print (result1, result2)
a. 8 5
b. 2 5
c. 5 2
d. 8 7
6 x= 12
def f1(a,b=x):
print (a,b)
x=15
f1(4)
a. Error
b. 12 4
c. 4 12
d. 4 15
7 def f():
global a
print (a)
a=”hello”
print (a)
a=”world”
f()
print (a)
8 def f1(a,b=[]):
b.append(a)
return b
print (f1(2,[3,4]))
a. [3,2,4]
b. [2,3,4]
c. Error
d. [3,4,2]
9 def f(p, q, r):
global s
p = 10
q = 20
r = 30
s = 40
print(p,q,r,s)
p,q,r,s = 1,2,3,4
f(5,10,15)
10 def f(x):
print("outer")
def f1(a):
print("inner")
print(a,x)
f(3)
f1(1)
11 x=5
def f1():
global x
x=4
def f2(a,b):
global x
return a+b+x
f1()
total = f2(1,2)
print(total)
12 x=100
def f1():
global x
x=90
def f2():
global x
x=80
print(x)
13 On assigning a value to a variable inside the function, it automatically becomes a
global variable. State True or False
14 What happens if a local variable exists with the same name as the global variable
you want to access?
a. Error
b. The local variable is shadowed
c. Undefined behavior
d. The global variable is shadowed
15 Which of the following is the use of function in python?
a. Functions are reusable pieces of programs
b. Functions don’t provide better modularity for your application
c. you can’t also create your own functions
d. All of the mentioned
16 Which keyword is used for function?
a. Fun
b. Define
c. Def
d. Function
17 x = 50
def func(x):
print('x is', x)
x=2
print('Changed local x to', x)
func(x)
print('x is now', x)
18 def say(message, times = 1):
print(message * times)
say('Hello')
say('World', 5)
19 def func(a, b=5, c=10):
print('a is', a, 'and b is', b, 'and c is', c)
func(3, 7)
func(25, c = 24)
func(c = 50, a = 100)
20 Which of the following refers to mathematical function?
a. sqrt
b. rhombus
c. add
d. area