You are on page 1of 11

Functions

Name the built-in function / method


Q1 Name the built-in mathematical function / method that is used to return an
absolute value of a number. (1)
A1 abs()
Theory based
Q1 What do you understand by local and global scope of variables? How can you
access a global variable inside the function, if function has a variable with same
name. (2)
A1 A global variable is a variable that is accessible globally. A local variable is one
that is only accessible to the current scope, such as temporary variables used in a
single function definition.
A variable declared outside of the function or in global scope is known as global
variable. This means, global variable can be accessed inside or outside of the
function where as local variable can be used only inside of the function. We can
access by declaring variable as global A. (2)
Q2 Differentiate between actual parameter(s) and a formal parameter(s) with a
suitable example for each.
A2 The list of identifiers used in a function call is called actual parameter(s) whereas
the list of parameters used in the function definition is called formal parameter(s).
Actual parameter may be value / variable or expression.
Formal parameter is an identifier.
Example:
def area(side): # line 1
return side*side;
print(area(5)) # line 2
In line 1, side is the formal parameter and in line 2, while invoking area() function, the
value 5 is the actual parameter.
A formal parameter, i.e. a parameter, is in the function definition. An actual
parameter, i.e. an argument, is in a function call.
Q3 Explain the use of global key word used in a function with the help of a suitable
example. (2)
A3 Use of global key word:

1
In Python, global keyword allows the programmer to modify the variable outside the
current scope. It is used to create a global variable and make changes to the
variable in local context. A variable declared inside a function is by default local and
a variable declared outside the function is global by default. The keyword global is
written inside the function to use its global value. Outside the function, global
keyword has no effect.
Example
c = 10 # global variable
def add():
global c
c = c + 2 # global value of c is incremented by 2
print("Inside add():", c)
add()
c=15
print("In main:", c)
output:
Inside add() : 12
In main: 15

Find and write the output


Q1Find and write the output of the following python code:
def fun(s):
k=len(s)
m=" "
for i in range(0,k):
if(s[i].isupper()):
m=m+s[i].lower()
elif s[i].isalpha():
m=m+s[i].upper()
else:
m=m+'bb'
print(m)
fun('school2@com') (2)

A1 SCHOOLbbbbCOM (2)

Q2Find and write the output of the following python code: (3)
def Change(P ,Q=30):
P=P+Q
Q=P-Q
print( P,"#",Q)

2
return (P)
R=150
S=100
R=Change(R,S)
print(R,"#",S)
S=Change(S)
A2
250 # 150
250 # 100
130 # 100 (3)

Q3Find and write the output of the following python code:


a=10
def call():
global a
a=15
b=20
print(a)
call() (2)
A3 15 (2)
Q4 Find and write the output of the following Python code:
def Display(str):
m=" "
for i in range(0,len(str)):
if(str[i].isupper()):
m=m+str[i].lower()
elif str[i].islower():
m=m+str[i].upper()
else:
if i%2==0:
m=m+str[i-1]
else:
m=m+"#"
print(m)
Display('Fun@Python3.0') (2)

A4 OUTPUT : fUNnpYTHON

3
Q5 Predict the output of the Python code given below:
def Diff(N1,N2):
if N1>N2:
return N1-N2
else:
return N2-N1
NUM= [10,23,14,54,32]
for CNT in range (4,0,-1):
A=NUM[CNT]
B=NUM[CNT-1]
print(Diff(A,B),'#', end=' ') (2)

A5
22 # 40 # 9 # 13 #
(½ mark for the correct digit with a #)
Q6 Write the output of the code given below: (2)
p=5
def sum(q,r=2):
global p
p=r+q**2
print(p, end= '#')
a=10
b=5
sum(a,b)
sum(r=5,q=1)
A6 Output:
105#6#
(1 mark for 105# and 1 mark for 6#)

Q7 Predict the output of the following code: (2)


def Changer (P,Q=10):
P= P/Q
Q = P%Q
return P
A=200

4
B=20
A=Changer(A,B)
print(A,B,sep =’$’)
B=Changer(B)
print(A,B,sep = ‘$’ ,end=’###’)
A7 10.0$20
10.0$2.0###

Write a user-defined function


Q1 Write a function LShift(Arr,n) in Python, which accepts a list Arr of numbers
and n is a numeric value by which all elements of the list are shifted to left.
Sample Input Data of the list
Arr= [ 10,20,30,40,12,11], n=2
Output
Arr = [30,40,12,11,10,20] (3)
A1
def LShift(Arr,n):
L=len(Arr)
for x in range(0,n):
y=Arr[0]
for i in range(0,L-1):
Arr[i]=Arr[i+1]
Arr[L-1]=y
print(Arr)

Q2 Write a function INDEX_LIST(L), where L is the list of elements passed as


argument to the function. The function returns another list named ‘indexList’ that
stores the indices of all Non-Zero Elements of L.
For example:
If L contains [12,4,0,11,0,56]
The indexList will have - [0,1,3,5] (3)
A2
def INDEX_LIST(L):

5
indexList=[]
for i in range(len(L)):
if L[i]!=0:
indexList.append(i)
return indexList
(½ mark for correct function header
1 mark for correct loop
1 mark for correct if statement
½ mark for return statement)

Q3 Write a function countNow(PLACES) in Python, that takes the dictionary,


PLACES as an argument and displays the names (in uppercase)of the places whose
names are longer than 5 characters.
For example, Consider the following dictionary
PLACES={1:"Delhi",2:"London",3:"Paris",4:"New York",5:"Doha"}
The output should be:
LONDON
NEW YORK (2)
A3
PLACES={1:”Delhi”, 2:”London”,3:”Paris”,4:”New York”,5:Dubai”}
def countNow(PLACES):
for place in PLACES.values( ) :
if len(place)>5:
print(place.upper( ) )
countNow(PLACES) (2)

Q4 Write a function, lenWords(STRING), that takes a string as an argument and


returns a tuple containing length of each word of a string.
For example, if the string is "Come let us have some fun", the tuple will have (4, 3, 2,
4, 4, 3)
A4
def lenWords(STRING):
T=()
L= STRING.split( )
for word in L:
length = len(word)
T= T+ (length ,)
return T

6
MCQ
Q1 Which of the following components are part of a function header in Python?
a. Function Name b. Return Statement c. Parameter List d. Both a and c (1)
A1 d. Both a and c (1)
Q2 Which of the following function header is correct?
a. def cal_si(p=100, r, t=2) b. def cal_si(p=100, r=8, t)
c. def cal_si(p, r=8, t) d. def cal_si(p, r=8, t=2) (1)
A2 d. def cal_si(p, r=8, t=2) (1)
Q3 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() (1)
A3 a. my_func() (1)
Q4 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
A4 d. None
Q5 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
A5 d. 250 300
Q6 What will be the output of the following code?
value = 50
def display(N):
global value
value = 25
if N%7==0:

7
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#
A6 b. 50#5 (2)

Q7 What is the output of the following code snippet?


def ChangeVal(M,N):
for i in range(N):
if M[i]%5 == 0:
M[i]//=5
if M[i]%3 == 0:
M[i]//=3
L = [25,8,75,12]
ChangeVal(L,4)
for i in L:
print(i,end="#")
a) 5#8#15#4# b) 5#8#5#4# c) 5#8#15#14# d) 5#18#15#4#
A7 b) 5#8#5#4# (1)
Q8 What will be the output of the following code?
x=3
def myfunc():
global x
x+=2
print(x, end=' ')
print(x, end=' ')
myfunc()
print(x, end=' ')
a. 3 3 3 b. 3 4 5 c. 3 3 5 d. 3 5 5

8
A8 d. 3 5 5 (2)
----------------------------------------------------------------------------------------------------------------
Q9 Consider the following directory structure.
Suppose root directory (School) and present working directory are the same. What
will be the absolute path of the file Syllabus.jpg?
a. School/syllabus.jpg
b. School/Academics/syllabus.jpg
c. School/Academics/../syllabus.jpg
d. School/Examination/syllabus.jpg

A9 b. School/Academics/syllabus.jpg (2)
Q10 Consider the code given below:
b=100
def test(a):
__________________ # missing statement
b=b+a
print(a,b)
test(10)
print(b)

Which of the following statements should be given in the blank for #Missing
Statement, if the output produced is 110?
Options:
a. global a b. global b=100 c. global b d. global a=100 (1)
A10 Option c
global b (1)

ASSERTION AND REASONING


Q1 is ASSERTION AND REASONING based question. Mark the correct choice as
(a) Both A and R are true and R is the correct explanation for A
(b) Both A and R are true and R is not the correct explanation for A
(c) A is True but R is False
(d) A is false but R is True
Assertion (A):- If the arguments in function call statement match the number and
order of arguments as defined in the function definition, such arguments are called
positional arguments.
9
Reasoning (R):- During a function call, the argument list first contains default
argument(s) followed by positional argument(s). (1)
A1 (c) A is True but R is False (1)
Q2 is ASSERTION AND REASONING based question. Mark the correct choice as
(a) Both A and R are true and R is the correct explanation for A
(b) Both A and R are true and R is not the correct explanation for A
(c) A is True but R is False
(d) A is false but R is True

Assertion(A): Python standard library consists of number of modules.


Reasoning(R): A function in a module is used to simplify the code and avoids
repetition. (1)
A2 Option b
Both A and R are true but R is not the correct explanation for A (1)
Rewrite the correct code and underline the corrections
Q1 Rao has written a code to input a number and check whether it is prime or not.
His code is having errors. Rewrite the correct code and underline the corrections
made.
def prime():
n=int(input("Enter number to check :: ")
for i in range (2, n//2):
if n%i=0:
print("Number is not prime \n")
break
else:
print("Number is prime \n’) (2)
A1
def prime():
n=int(input("Enter number to check :: ")) #bracket missing
for i in range (2, n//2):
if n%i==0: # = missing
print("Number is not prime \n")
break #wrong indent
else:
print("Number is prime \n”) # quote mismatch
(½ mark for each correct correction made and underlined.)

10
Q2 The code given below accepts a number as an argument and returns the reverse
number. Observe the following code carefully and rewrite it after removing all syntax
and logical errors. Underline all the corrections made. (2)
define revNumber(num):
rev = 0
rem = 0
While num > 0:
rem ==num %10
rev = rev*10 + rem
num = num//10
return rev
print(revNumber(1234))
A2
def revNumber(num):
rev = 0
rem = 0
while num > 0:
rem =num %10
rev = rev*10 + rem
num = num//10
return rev
print(revNumber(1234))

½ mark for each correction made (2)

11

You might also like