Functions Worksheet

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

Class: XII else:

Chapter 3 – Working with Functions – Worksheet m=m+'##'


Write the output of the code given below: print(m)
1) fun('Charles@Babbage')
def sample(s):
n = len(s) 4)
m="" def exam(s):
for i in range(0, n): r=""
if (s[i] >= 'a' and s[i] <= 'm'): for i in s:
m = m +s[i].upper() if i.isupper()== False:
elif (s[i] >= 'n' and s[i] <= 'z'): r+=i.lower()
m = m +s[i-1] elif i.islower()== False:
elif (s[i].isupper()): r+=i.upper()
m = m + s[i].lower() else:
else: r+=i
m = m +'&' print(r)
print(m) exam("Preboard Exam 2023-2024")
sample("welcome2Python")
5)
2) s="Cbse Exam 2023-2024"
p=5 print(s)
def sum(q,r=2): r=""
global p for i in s:
p=r+q**2 if i.isupper():
print(p, end= '#') r+=i.lower()
a=10 elif i.islower():
b=5 r+=i.upper()
sum(a,b) elif i.isdigit():
sum(r=5,q=1) r+=str(int(i)+1)
else:
3) r=r+i
def fun(s): print(r)
k=len(s)
m="" 6)
for i in range(0,k): T= "Peace#mind?"
if(s[i].isupper()): R=""
m=m+s[i].lower() l=len(T)
elif(s[i].isalpha()): print("Orignal : ",T)
m=m+s[i].upper() for i in range(l):
if T[i].isalpha()==False: Functions – Output Prediction
R+='*' 1. What will be the output of the following Python code?
elif T[i].isupper()==True: def sayHello():
R+=chr(ord(T[i])+1) print('Hello World!')
else: sayHello()
R+= T[i+1] sayHello()
print("Final : ",R)
2. What will be the output of the following Python code?
7) def printMax(a, b):
poet= "WiLLiaMWordsWoRtH" ; if a > b:
l=len(poet) print(a, 'is maximum')
r="" elif a == b:
print("Original : ",poet) print(a, 'is equal to', b)
for i in range(l): else:
if poet[i].islower(): print(b, 'is maximum')
r+=poet[i-1] printMax(3, 4)
elif poet[i].isupper():
if poet[i]=='S': 3. What will be the output of the following Python code?
r+='X'; x = 50
elif(poet[i]=='E'): def func(x):
r+=poet[i-1].upper() print('x is', x)
else: x=2
r+=chr(ord(poet[i])-1) print('Changed local x to', x)
print(" Final : ",r) func(x)
print('x is now', x)
8) 4. What will be the output of the following Python code?
def Execute(M): x = 50
if M%3==0: def func():
return M*3 global x
else: print('x is', x)
return M+10 x=2
def Output(B=2): print('Changed global x to', x)
for T in range (0,B): func()
print(Execute(T),"*",end="") print('Value of x is', x)
print() 5. What will be the output of the following Python code?
Output(4) def say(message, times = 1):
Output() print(message * times)
Output(3) say('Hello')
say('World', 5)
6. What will be the output of the following Python code? print(i)
def func(a, b=5, c=10): 12. What will be the output of the following Python code?
print('a is', a, 'and b is', b, 'and c is', c) def a(b):
func(3, 7) b = b + [5]
func(25, c = 24) c = [1, 2, 3, 4]
func(c = 50, a = 100) a(c)
7. What will be the output of the following Python code? print(len(c))
def maximum(x, y): 13. What will be the output of the following Python code?
if x > y: a=10
return x b=20
elif x == y: def change():
return 'The numbers are equal' global b
else: a=45
return y b=56
print(maximum(2, 3)) change()
8. What will be the output of the following Python code? print(a)
def cube(x): print(b)
return x * x * x 14. What will be the output of the following Python code?
x = cube(3) def change(i = 1, j = 2):
print(x) i=i+j
9. What will be the output of the following Python code? j=j+1
def C2F(c): print(i, j)
return c * 9/5 + 32 change(j = 1, i = 2)
print(C2F(100)) 15. What will be the output of the following Python code?
print (C2F(0)) def display(b, n):
10. What will be the output of the following Python code? while n > 0:
def power(x, y=2): print(b,end="")
r=1 n=n-1
for i in range(y): display('z',3)
r=r*x 16. What will be the output of the following Python code?
return r def foo(k):
print(power(3)) k[0] = 1
print(power(3, 3)) q = [0]
11. What will be the output of the following Python code? foo(q)
i=0 print(q)
def change(i): 17. What will be the output of the following Python code?
i=i+1 def foo(fname, val):
return i print(fname(val))
change(1) foo(max, [1, 2, 3])
foo(min, [1, 2, 3]) in different list eg. if the array initially contains 2, 4, 1, 6, 5, 7, 9, 2, 3, 10
18. What will be the output of the following Python code? then it should contain 4, 2, 6, 1, 7, 5, 2, 9, 10, 3
def foo(): 7. Define a function ZeroEnding(SCORES) to add all those values in the list
return total + 1 of SCORES, which are ending with zero (0) and display the sum. For
total = 0 example : If the SCORES contain [200, 456, 300, 100, 234, 678] The sum
print(foo()) should be displayed as 600
19. What will be the output of the following Python code? 8. Write a Python function SwitchOver(Val) to swap the even and odd
def foo(): positions of the values in the list Val. Note : Assuming that the list has even
total += 1 number of values in it. For example : If the list Numbers contain
return total [25,17,19,13,12,15] After swapping the list content should be displayed as
total = 0 [17,25,13,19,15,12]
print(foo()) 9. Write a function R_Shift(Arr,n) in Python, which accepts a list Arr of
20. What will be the output of the following Python code? numbers and n is a numeric value by which all elements of the list are
def foo(i, x=[]): shifted to right and the last element removed should be added in the
x.append(i) beginning. Sample Input Data of the list Arr=[ 1,2,3,4,5,6], n=2 Output Arr =
return x [5, 6, 1, 2, 3, 4]
for i in range(3): 10. Write definition of a function How_Many(List, elm) to count and
print(foo(i)) display number of times the value of element is present in the List. (Note:
2 Mark questions related to functions don’t use the count() function) For example : If the Data contains
1. Write a function listchange(Arr,n)in Python, which accepts a list Arr of [205,240,304,205,402,205,104,102] and element contains 205 The
numbers and n is an numeric value depicting length of the list. Modify the function should display 205 found 3 Times
list so that all even numbers doubled and odd number multiply by 3 11. Write definition of a Method MSEARCH(STATES) to display all the state
Sample Input Data of the list: Arr= [ 10,20,30,40,12,11], n=6 Output: Arr = names from a list of STATES, which are starting with alphabet M. For
[20,40,60,80,24,33] example: If the list STATES contains[“MP’,”UP”,”MH”,”DL”,”MZ”,”WB”]
2. Python function oddeve(L) to print positive numbers in a list L. Example: The following should get displayed MP MH MZ
Input: [4, -1, 5, 9, -6, 2, -9, 8] Output: [4, 5, 9, 2, 8] 12. Write a function listchange(Arr)in Python, which accepts a list Arr of
3. Write definition of a method/function AddOddEven(VALUES) to display numbers, the function will replace the even number by value 10 and
sum of odd and even values separately from the list of VALUES. For multiply odd number by 5.
example : If the VALUES contain [15, 26, 37, 10, 22, 13] The function Sample Input Data of the list is: a=[10,20,23,45] output : [10, 10, 115, 225]
should display Even Sum: 58 13. Write a function L_Shift(Arr,n) in Python, which accepts a list Arr of
Odd Sum: 65 numbers and n is a numeric value by which all elements of the list are
4. Write the definition of a function Reverse(X) in Python to display the shifted to left and the last element removed should be added in the end.
elements in reverse order such that each displayed element is twice of the Sample Input Data of the list Arr=[ 1,2,3,4,5,6], n=2 Output Arr =
original element (element *2) of the List X [3,4,5,6,1,2]
5. Write a function INDEX_LIST(L), where L is the list of elements passed as 14. Write a function INDEX_LIST(L), where L is the list of elements passed
argument to the function. The function returns sum of odd numbers in list. as argument to the function. The function returns another list named
6. Write a function in Shift(Lst), Which accept a List ‘Lst’ as argument and ‘indexList’ that stores the indices of all Non-Zero Elements of L. For
swaps the elements of every even location with its odd location and store example:
If L contains [12,4,0,11,0,56] The indexList will have - [0,1,3,5]
Reasoning (R):- During a function call, the argument list first contains
default argument(s) followed by positional argument(s).

Assertion (A): Built-in function are predefined in the language that are
used directly.
Reason (R): print() and input() are built-in funcitions

Assertion (A); A variable is still valid if it is not defined inside the function.
The values defined in global scope can be used.
Reasoning (R): Python used LEGB rule to resolve the scope of a variable.

Assertion (A): When passing a mutable sequence as an argument, function


Q17 and 18 are ASSERTION AND REASONING based questions. Mark the modifies the original copy of the sequence.
correct choice as: Reasoning (R): Function can alter mutable sequences passes to it.
(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 Assertion (A):- Key word arguments are related to function calls.
(c) A is True but R is False Reasoning(R ):- When you use keyword arguments in function call, the
(d) A is false but R is True caller identifies the arguments by the parameter name.

Assertion (A): Global variable is declared outside the all the functions.
Reasoning (R): It is accessible through out all the functions.

Assertion (A):- All the keyword arguments passed must match one of the
arguments accepted by the function
Reasoning (R):- You cannot change the order of appearance of the
keyword.

Assertion (A): The math.pow(2,4)gives the output: 16.0


Reason (R): The math.pow() method receives two float arguments, raise
the first to the second and return the result.

Assertion (A):- Keyword arguments are related to function call statement.


Reasoning (R):- When we use keyword argument in a function call, the
caller identifies the argument by the parameter.

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.

You might also like