CH 2 Functions

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

Computer Science (083)

Chapter 2- Functions

SECTION A- 1 mark each


1 State True or False
“We can pass variable number of arguments to a function.”
2 State True or False
“The default value assigned to a parameter should be immutable ”
3 Which of the following function header is wrong?
a) def findsum(a,b=5,c=10):
b) def findsum(a=3,b=5,c=10):
c) def findsum(a=3,b=5,c):
d) def findsum(a,b,c):
4 Which of the following is the correct way to call a function?
a) myfun()
b) def myfun()
c)return myfun
d) call myfun()
5 Observe the following function definition and find the statement which will not report an
error from the given options
def myfun(a,b):
print(a,b,sep='#')
a) myfun(5,a=10)
b) myfun(10,b=20)
c) myfun(b=5,10)
d) myfun(a=10,20)
6 Which of the following is not a valid keyword in Python?
a)return b) def c) global d) total
7 Write correct statement to import sqrt function from math module.
_____________
print(sqrt(25))
8 Order of execution of the given program is
1. def choose(a,b):
2. if a>b:
3. return a
4. else:
5. return b
6. a,b=20,10
7. c=choose(a,b)
8. print(c)
a) 1 -> 6 -> 7 -> 1 -> 2 -> 3 -> 4 -> 5 -> 7 -> 8
b) 1 -> 6 -> 7 -> 1 -> 2 -> 3 -> 7 -> 8
c) 6 -> 7 -> 1 -> 2 -> 3 -> 7 -> 8
d) 1 -> 2 -> 3 -> -> 3 -> 6 -> 7 -> 8
9 What will be the output of the following Python code?
def calc (num1, num2):
tot= num1 + num2
tot=calc(10,50)
print(tot)
a) 60
b) 0
c) Null
d) None
10 def myfunc(x):
________
x=x+10
y=y+10
return x+y
x,y=210,200
z=myfunc(x)
print(z)Which of the following statements should be given in the blank for #Missing
Statement, if the output produced is 430?
Options:
a) global y
b) global y=200
c)global x
d) global x,y
11 Identify the incorrect statement from the following:
a) Any valid identifier can be used as function name
b) Non-default arguments cannot follow default arguments in function header
c) Formal parameter can be a value , an identifier or an expression
d) Positional Arguments cannot follow keyword arguments in function call
12 Which of the following is not an advantage of functions:
a) Program becomes more readable
b) we can avoid rewriting the same code again and again in a program.
c) Easy to test and debug
d) Function can alter immutable type values
Q13,14 and 15 are ASSERTION AND REASONING based questions. 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
13 Assertion: collection of related functions make a module
Reason: It is possible to import only a single function from a module
14 Assertion: return statement in python specifies what value is to be returned back to the
calling function
Reason: It is not possible to return multiple values from a function

15 Assertion: A tuple value cannot be modified in a function


Reason: Function can alter only mutable types
SECTION B- 2 marks each
16 Gopal is a Python programmer working on a program to find and return the sum of all the odd
values in a list. The code written below has syntactical errors. Rewrite the correct code and
underline the corrections made.
def sumodd(L):
s=0
for n in L
if n%2!=0:
s=+n
return sum
L=[2,5,6,7,9,3]
sumodd(L)
print(s)

17 Rewrite the following code in Python after removing all syntax error(s). Underline each
correction done in the code.
def Tot(Number) #Method to find Total
Sum=0
for C in Range (1, Number+1):
Sum+=C
RETURN Sum
print (Tot[3]) #Function Call

18 Differentiate between actual parameter(s) and a formal parameter(s) with a suitable example
for each.
19 Explain the use of global keyword used in a function with the help of a suitable example.
20 What will be the output of the following code?
def find(var1=10, var2=20):
var1+=1
var2 = var2 - 2
return var1+var2
print(find(30),find())

21 Can a function return multiple values? If yes explain with example


22 Write the output of the code given below:
def ChangeVal(M,N):
for i in range(N):
if M[i]%2 == 0:
M[i]//=2
if M[i]%3 == 0:
M[i]//=3
L = [9,18,16,12]
ChangeVal(L,4)
print(L)

23 Find the output of the following python code:


x,y = 10,20
def myfunc():
global x
x=100
y=200
print(x,end='#')
print(y)
myfunc()
print(x,end='#')
print(y)
24 Write a method in python to find and display the prime numbers between 2 to N.
Pass N as argument to the method.
25 Find the output for the following:
def calc(num1,num2=30):
num1%=10
num2//=10
return num1,num2
a,b=calc(45)
print(a,b)
26 Write a function SWAP (num) in Python, which accepts a list num of integers, and interchange
the adjacent elements of the list and print the modified list as shown below: (Number of
elements in the list is assumed as even)
Original List:
num = [8,7,10,9,12,11]
After Rearrangement num = [7,8,9,10,11,12]
27 Write a function FIND(ITEM) in Python, that takes the dictionary, ITEM as an argument and
displays the item name (in uppercase) which has atleast 5 characters and price less than 50.
For example, Consider the following dictionary
ITEM={'Pen':10,'Pencil':5,'Ink':50,'Files':45}
The output should be:
PENCIL
FILES
28 Write a function, Findlen(STR), that takes a string as an argument and returns a list
containing each word of the string which are larger than 4 characters.
For example, if STR contains "CBSE 2023-24 CS examination", the list will have
['2023-24','examination']
29 Write a function, wordlength(STR), that takes a string as an argument and returns a
dictionary containing each word of the string and it's length as key value pair.
For example, if STR contains "CS with Python", the dictionary will have
{'CS':2,'with':4,'Python':6}
30 Write a function CALC(MLIST) in Python, that takes the dictionary, MLIST as an argument and
displays the name and mark of those students whose mark is less than 50.
For example, Consider the following dictionary
MLIST={'Devan':40,'Joseph':55,'Jimmy':48,'Susan':95}
the output will be
Devan
Jimmy

31 Write a function DISPLAY(ELIST) in Python, that takes the dictionary,ELIST as an argument


and displays the places of those employees whose place starts with 'G'(do not consider case).
For example, Consider the following dictionary
ELIST={'David':'Goa','John':'Mumbai','Jimmy':'Gujarat','Syam':'Chennai'}
the output will be
Goa
Gujarat
32 Write the definition of a method ZeroEnding(SCORES) to add all those values in the list of
SCORES, which are ending with zero (0) and display the sum.
For example :
If the SCORES contain [200, 456, 300, 100, 234, 678]
The sum should be displayed as 600
33 Write a definition of a method COUNTNOW(PLACES) to find and display those place names, in
which there are more than 5 characters.
For example :
If the list PLACES contains
["DELHI","LONDON","PARIS","NEW YORK","DUBAI"]
The following should get displayed :
LONDON
NEW YORK
34 Write definition of a method/function Howmany(NUMLIST,N) to count
and display number of times the value of N is present in the list NUMLIST.
For example :
If NUMLIST=[25,42,36,25,12,25,49,36]
and N is 25
The function should display
25 found 3 times
35 What is meant by scope and lifetime of variables? Write any 2 python scope

SECTION C- 3 marks each


36 a) What is meant by built-in functions? Give any 2 examples.
b) Write a program to display the square of a number which is passed to the function
square() as argument. Then identify and comment the function header and function call.
37 Predict the output of the following code for the function calls given below :
a) Find(12,5) b) Find(12,3) c) Find(12,0)
def Find(a,b):
try:
c=a%b
r=a/c
print(c,r,sep='#')
except:
print('Error')
finally:
print('Done')

38 Find the output for the following:


s="Practice-2"
def change(s):
m=""
for i in range(0, len(s)):
if s[i] in 'aeiouAEIOU':
m = m +s[i].upper()
elif s[i].isupper() :
m = m +s[i+1]
elif s[i].isdigit():
m = m +str(int(s[i])+1)
elif not s[i].isalnum():
m = m +'*'
else:
m=m+s[i]
print(m)
change(s)
39 Write outputs of the following :
a)
def update(S,grace):
S['Mark']+=grace
student={'Name':'Syam','Mark':80}
update(student,12)
print(student)
b)
def update(S,alpha):
S=S+alpha
string='abcd'
update(string,'e')
print(string)
c)
def update(L,item):
L=L+[item]
NUM=[3,4,5,6]
update(NUM,7)
print(NUM)
40 Find and write the output of the following Python code:
def Changer(P=100,Q=5):
P=P//Q
Q=P%Q
print(P,'#',Q)
return P
A,B=50,20
A=Changer(A,B)
B=Changer(B)
A=Changer()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy