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

CHAPTER- FUNCTIONS

1. 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
2. Write the output of the code given below:
p=5
def sum(q,r=2):
global p
p=r+q**2
print(p, end= ‘#’)
a=10
b=5
print(p, end = ‘#’)
sum(a,b)
sum(r=5,q=1)
print(p, end = ‘#’)
3. Predict the output of the Python code given below:
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()
elif i%2==0:
m=m+str[i-1]
else:
m=m+"#"
print(m)
Display('Welcome@Python3.0')

4.
5.

6. Vivek has written a code to input a number and check whether it is even or
odd number. His code is having errors. Rewrite the correct code and underline
the corrections made.
Def checkNumber(N):
status = N%2
return
#main-code
num=int( input(“ Enter a number to check :))
k=checkNumber(num)
if k = 0:
print(“This is EVEN number”)
else:
print(“This is ODD number”)
7. Write a function lenFOURword(L), where L is the list of elements (list of
words) passed as argument to the function. The function returns another list
named ‘indexList’ that stores the indices of all four lettered word of L.
For example:
If L contains [“DINESH”, “RAMESH”, “AMAN”, “SURESH”, “KARN”]
The indexList will have [2, 4]
8. Write the output of the code given below:
def printMe(q,r=2):
p=r+q**3
print(p)
#main-code
a=10
b=5
printMe(a,b)
printMe(r=4,q=2)
9. We can pass the argument in the function call in any order using…
a. Keyword argument b. Variable Length argument
c. No argument d. D. default argument
10. Which of the arguments can be skipped in the function call?
a. Positional arguments b. Default arguments
c. Keyword arguments d. No arguments
11. Sameer has written a python function to compute the reverse of a number.
He has however committed a few errors in his code. Rewrite the code after
removing errors also underline the corrections made.
define reverse(num):
rev = 0
While num > 0:
rem == num %10
rev = rev*10 + rem
num = num//10
return rev
print(reverse(1234))
12. Predict the output of the following python code:
def foo(s1,s2):
l1=[]
l2=[]
for x in s1:
l1.append(x)
for x in s2:
l2.append(x)
return l1,l2
a,b=foo("FUN",'DAY')
print(a,b)
13. Write a function modilst(L) that accepts a list of numbers as argument and
increases the value of the elements by 10 if the elements are divisible by 5.
Also write a proper call statement for the function.
For example:
If list L contains [3,5,10,12,15]
Then the modilist() should make the list L as [3,15,20,12,25]

14. Write the output of the following code:


def change(m, n=10):
global x
x+=m
n+=x
m=n+x
print(m,n,x)
x=20
change(10)
change(20)
15. Which of the following function headers is correct?
A. def fun(a = 5, b =4, c)
B. def fun(a = 5, b, c =4)
C. def fun(a, b = 5, c = 4)
D. def fun(a, b, c = 4, d)
16. Shivam wants to know the correct name resolution rule in Python. Please
help him.
a. Local, Enclosing, Global, Built in b. Global, Enclosing, Local, Built in
c. Local, Global, Enclosing, Built in d. Built in, Enclosing, Global, Local
17. Which of the following function call can be used to invoke the below function
Definition : def calc(p,q,r,s)
I. calc(3,4,5,6) II. calc (p=1,2,3,5)
III. calc(3, 4, r=3, s=5) IV. calc(q=4, p=3,s=5,r=7)

a. All are correct b. I, III, IV are correct


c. I, II ,III are correct d. I & IV are correct

18. (c)

19. Riya was asked to accept a list of even numbers ,but she did not put the
relevant condition while accepting the list of numbers. She wrote a user
defined function odd to even (L) that accepts the list L as an argument and
converts all the odd numbers into even by multiplying them by 2.
def oddtoeven (L)
for i in range (size(L)):
if (L[i]%2! == 0)
L[i]= L[1] ** 2
print (L)
There are some errors in the code. Rewrite the correct code.
20. Write a user defined function in python which taken an integer n as
argument and return the count of all numbers with unique digits, say x,
where 0 <= x < 10n.
For Example:
If the Input n=2
Then the output should be 91
Explanation: The answer should be the total numbers in the range
0<=x<100, excluding 11,22,33,44,55,66,77,88,99
21. Write the definition of a function Reverse (x) in Python, to display the
elements in reverse order such that each displayed element is the twice of
the original element (element *2) of the List x in the following manner:
Example : If List x contains 7 integers is as follows:
x [0] x [1] x [2] x [3] x [4] x [5] x [6]
4 8 7 5 6 2 10
After executing the function, the array content should be displayed as follows:
x [0] x [1] x [2] x [3] x [4] x [5] x [6]
20 4 12 10 14 16 8
22. Carefully observe the following Python code and answer the question that
follow:
x=5
def func2():
global x
x=x+1
print(x)
x=3
func2()
print(x)
On execution the above code, produces the following output:
6
3
Explain the output with respect to the scope of the variables.

23. Write a Python function named find_Average(scores) that takes a list of


scores as an argument. The function should calculate and return the average of
the scores. Additionally, display the scores that are above the calculated
average. For example, consider the following list of scores:
scores = [85, 92, 78, 95, 88]
The function should return the average (rounded to two decimal places) and
print the scores that are above the calculated average. (Note: Do not use in-
built functions)

24. Write a Python function named remove_Duplicates(word)that takes a string


as an argument. The function should return a new string with duplicate
characters removed, preserving the order of the remaining characters.
For example, consider the following word:
word = "programming"
The function should return a new string with duplicates removed: "progamin"

25. Predict the output of the Python code given below:


def Diff(N1,N2):
if N1>N2:
return N1-N2
else:
return N2-N1
NUM= [8,20,1,4,3]
for CNT in range (4,0,-1):
A=NUM[CNT]
B=NUM[CNT-1]
print(Diff(A,B),’*’, end=’ ‘)
26. Write definition of a Method AFIND(CITIES) to display all the city names
from a list of CITIES, which are starting with alphabet A. For example: If the list
CITIES contains [“AHMEDABAD”,”CHENNAI”,”NEW DELHI”,”AMRITSAR”,”AGRA”]
The following should get displayed AHEMDABAD AMRITSAR AGRA
27. Consider the code given below and Identify Which message will never be
printed
def prog(name):
for x in name:
if x.isalpha():
print(“alphabet”)
elif x.isdigit():
print(“digit”)
elif x.isupper():
print(“upper”)
else:
print(“allthebest”)
prog(“vishal123@gmail.com”)

A. alphabet B. digit C. upper D. allthebest


28. Assertion(A): Keyword argument were the named arguments with assigned
values being passed in the function call.
Reasoning (R): Default arguments cannot be skipped while calling the
function while keyword arguments are in function call statement.
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
29. Write the output for the following python code:
def Change_text(Text):
T=""
for K in range(len(Text)):
if Text[K].isupper():
T=T+Text[K].lower();
elif K%2==0:
T=T+Text[K].upper()
else:
T=T+T[K-1]
print(T)
Text="Good Go Head"
Change_text(Text)

30.

You might also like