0% found this document useful (0 votes)
418 views7 pages

Class 12 Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
418 views7 pages

Class 12 Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Functions computer science class 12 .

From this topic M[i]//=5


functions 1 mark, 2 marks, 3 marks and 4 marks will be if M[i]%3 == 0:
asked. M[i]//=3
L = [25,8,75,12]
[1] Which of the following function header is correct? ChangeVal(L,4)
a. def cal_si(p=100, r, t=2) for i in L:
b. def cal_si(p=100, r=8, t) print(i,end="#")
c. def cal_si(p, r=8, t) a) 5#8#15#4#
d. def cal_si(p, r=8, t=2) b) 5#8#5#4#
c) 5#8#15#14#
[2] Which of the following is the correct way to call a d) 5#18#15#4#
function?
a. my_func() [7] Find the output of the following code:
b. def my_func() def convert():
c. return my_func Name="PythoN3.10"
d. call my_func() R=""
for x in range(len(Name)):
[3] What will be the output of the following code? if Name[x].isupper():
def my_func(var1=100, var2=200): R=R+Name[x].lower()
var1+=10 elif Name[x].islower():
var2 = var2 - 10 R=R+Name[x].upper()
return var1+var2 elif Name[x].isdigit():
print(my_func(50),my_func()) R=R+Name[x-1]
a. 100 200 else:
b. 150 300 R=R+"#"
c. 250 75 print(R)
d. 250 300 a. pYTHOn##10
b. pYTHOnN#1
[4] What will be the output of the following code? c. pYTHOn#.1
value = 50 d. pYTHOnN#.1
def display(N):
global value [8] What will be the output of the following code?
value = 25 x=3
if N%7==0: def myfunc():
value = value + N global x
else: x+=2
value = value - N print(x, end=' ')
print(value, end="#") print(x, end=' ')
display(20) myfunc()
print(value) print(x, end=' ')
a. 50#50 a. 3 3 3
b. 50#5 b. 3 4 5
c. 50#30 c. 3 3 5
d. 5#50# d. 3 5 5

[5] What will be the output of the following code? [9] What will be the output of the following Python code?
import random def add (num1, num2):
List=["Delhi","Mumbai","Chennai","Kolkata"] sum = num1 + num2
for y in range(4): sum = add(20,30)
x = random.randint(1,3) print(sum)
print(List[x],end="#") a. 50
a. Delhi#Mumbai#Chennai#Kolkata# b. 0
b. Mumbai#Chennai#Kolkata#Mumbai# c. Null
c. Mumbai# Mumbai #Mumbai # Delhi# d. None
d. Mumbai# Mumbai #Chennai # Mumbai
[10] Assertion (A):- If the arguments in function call
[6] What is the output of the following code snippet? statement match the number and order of arguments as
def ChangeVal(M,N): defined in the function definition, such arguments are
for i in range(N): called positional arguments.
if M[i]%5 == 0:
Reasoning (R):- During a function call, the argument list print (T, end='@')
first contains default argument(s) followed by positional Change (S)
argument(s). print (S)
(a) Both A and R are true and R is the correct explanation a) WELcOME@ HELLO
for A b) HELLO@HELLO
(b) Both A and R are true and R is not the correct c) HELLO@WELcOME
explanation for A d) WELCOME@WELCOME
(c) A is True but R is False
(d) A is false but R is True [17] Identify the correct possible output for the following
Python code :
[11] Which of the following is not correct in the context of import random
Positional and Default parameters in Python functions? for N in range (2,5,2):
a) Default parameters must occur to the right of print (random. randrange (1,N) ,end="#")
Positional parameters a) 1#3#5#
b) Positional parameters must occur to the right of b) 2#3#
Default parameters c) 1#4#
c) Positional parameters must occur to the left of Default d) 1#3#
parameters
d) All parameters to the right of a Default parameter must [18] What are the possible outcome(s) executed from the
also have default values following code? Also specify the maximum and minimum
values that can be assigned to variable COUNT.
[12] For a function header as follows:
def Calc (X, Y=20) TEXT="CBSEONLINE"
Which of the following function calls will give an Error? COUNT=random.randint(0,3)
a) Calc (15,25) C=9
b) Calc (X=15,Y=25) while TEXT[C]!='L':
c) Calc (Y=25) print (TEXT[C]+TEXT[COUNT]+'*', end='')
d) Calc (X=25) COUNT=COUNT+1
C=C-1
[13] Which of the following is not correct in the context of a) EC*NB*IS*
scope of variables ? b) NS*IE*LO*
a) global keyword is used to change the value of a global c) ES*NE*IO*
variable in a local scope d) LE*NO*ON*
b) local keyword is used to change the value of a local
variable in a global scope
c) global variables can be accessed without using the [19] What will be the output of the following Python code
global keyword in a local scope ?
d) local variables cannot be used outside its scope def FunStr (S):
T=''
[14] Which of the following is not a function/method of for i in S:
the random module in Python? if i.isdigit ():
a) randfloat () T = T +i
b) randint () return T
c) random() X= "PYTHON 3.9"
d) randrange () Y = FunStr (X)
print (X, Y, sep="*")
[15] Identify the output of the following Python a) PYTHON 3.9
statements: b) PYTHON 3.9*3.9
S =”GoOD MORNING” c) PYTHON 3.9*39
print (S.capitalize (),s.title () ,end=”! “) d) Error
(a) GOOD MORNING !Good morning
(b) Good Morning!Good morning [20] What will be the output of the following Python
(c) Good morning! Good Morning! code?
(d) God morning Good Morning! V=50
def Change (N):
[16] What will be the output of the following Python global V
code? V, N = N, V
S="WELcOME" print (V, N, sep="#",end="@")
def Change (T) : Change (20)
T="HELLO" print (V)
a) 20#50@20 Functions Class 12 Computer Science 2 Marks Questions
b) 50@20#50 [1] Rewrite the following code in python after removing
c) 50#50#50 all syntax error(s). Underline each correction done in the
d) 20@50#20 code.
def Sum(Count) #Method to find sum
[21] What is the output of the following Python code ?ff S=0
def ListChange () : for I in Range(1,Count+1):
for i in range (len (L)): S+=I
if L[i]%2 == 0: RETURN S
L[i]=L[i]*2 print (Sum[2]) #Function Call
if L[i]%3==0: print (Sum[5])
L[i]=L[i]*3 Ans.:
else: def Sum(Count): #Method to find sum
L[i]=L[ij*5 S=0
L = [2,6,9,10] for I in range(1,Count+1):
ListChange () S+=I
for i in L: return S
print (i,end="#") print (Sum(2)) #Function Call
a) 4#12#27#20# print (Sum(5))
b) 20#36#27#100#
c) 6#18#27#50# [2] Find the output for the following:
d) Error def fun(s):
k=len(s)
[22] What will be the output of the following Python m=" "
code? for i in range(0,k):
V = 25 if(s[i].isupper()):
def Fun (Ch): m=m+s[i].lower()
V=50 elif s[i].isalpha():
print (V, end=Ch) m=m+s[i].upper()
V *= 2 else:
print (V, end=Ch) m=m+'#'
print (V, end="*") print(m)
Fun ("!") fun('BoardExam@2K23')
print (V)
a) 25*50! 100 !25 [3] Find and write the output of the following python code
b) 50*100 !100!100 :
c) 25*50! 100!100 def Changer(P,Q=10):
d) Error P=P/Q
Q=P%Q
[23] Predict the output of the Python code given below: print(P,"#",Q)
def Diff(N1,N2): return P
if N1>N2: A=200
return N1-N2 B=20
else: A=Changer(A,B)
return N2-N1 print( A,"$",B)
NUM= [10,23,14,54,32] B=Changer(B)
for CNT in range (4,0,-1): print( A,"$",B)
A=NUM[CNT] A=Changer(A)
B=NUM[CNT-1] print (A,"$",B)
print(Diff(A,B),'#', end=' ') [4] What possible outputs(s) are expected to be displayed
a) 22 # 40 # 9 # 13 # on screen at the time of execution of the program from
b) 9 # 22 # 13 # 53 # the following code? Also specify the maximum values that
c) 9 # -30 # 22 # 22 can be assigned to each of the variables FROM and TO.
d) Error import random
AR=[20,30,40,50,60,70]
FROM=random.randint(1,3)
TO=random.randint(2,4)
for K in range(FROM,TO+1):
print (AR[K],end=”#“)
(i) 10#40#70#
(ii) 30#40#50# for i in range (2, n//2):
(iii) 50#60#70# if n%i==0:
(iv) 40#50#70# print("Number is not prime \n")
break
[6] Differentiate between actual parameter(s) and formal else:
parameter(s) with a suitable example for each. print("Number is prime \n")
Ans.: break
[10] 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
[7] Explain the use of a global keyword used in a function
b=5
with the help of a suitable example.
sum(a,b)
Ans.: The global keyword is used to access the
sum(r=5,q=1)
variable written in top level statement or outside the
function.
Example: [11] Write a Python method/function SwapParts(Word) to
a=4 swap the first part and the second part of the string
def add(): Word. Assuming there are an even number of letters in
global a the string Word. The function should finally display the
return a+5 changed Word.
print(add())
For example :
[8] Find and write the output of the following Python
code: If Word = ‘Elephant’ then the function should convert
def Display(str): Word to ‘hantElep’ and display the output as:
m=""
for i in range(0,len(str)): Changed Word is hantElep
if(str[i].isupper()):
m=m+str[i].lower() Ans.:
elif str[i].islower():
m=m+str[i].upper() def SwapParts(word):
else: l=len(word)
if i%2==0: hf=l//2
m=m+str[i-1] nw=''
else: if l%2==0:
m=m+"#" nw=word[hf:]+word[:hf]
print(m) return nw
Display('Fun@Python3.0')
w=input("Enter the word:")
[9] Rao has written a code to input a number and check print(SwapParts(w))
whether it is prime or not. His code is having errors. [12] Write a Python method/function Noun2Adj(Word)
Rewrite the correct code and underline the corrections which checks if the string Word ends with the letter ‘y’. If
made. so, it replaces the last letter ‘y’ with the string ‘iful’ and
then displays the changed Word. For example if the Word
def prime(): is “Beauty”, then the Word should be changed to
n=int(input("Enter number to check :: ") “Beautiful”. Otherwise it should display Not ending with
for i in range (2, n//2): “y”.
if n%i=0:
print("Number is not prime \n") Ans.:
break
else: def Nount2Adj(word):
print("Number is prime \n’) nw=''
Ans.: rw='iful'
if word.endswith('y'):
def prime(): word=word[:-1]+rw
n=int(input("Enter number to check :: ")) else:
print("Word not ending with y.") count=0
return word for j in range(2,i//2+1):
w=input("Enter word endwith y") if i%j==0:
print(Nount2Adj(w)) count+=1
[13] Write the output for the following: if count>=1:
print(i,end=',')
a=10 n=int(input("Enter the value:"))
def call(): dis_CompoSite(n)
global a
a=15 [2] Write the definition of a method/function
b=20 TenTimesEven(VALUES) to add and display the sum of ten
print(a) times the even values present in the list of VALUES.
call() For example, If the Nums contain [5,2,3,6,3,4]
The method/function should display Sum: 120
[14] What do you understand by local and global scope of Ans.:
variables? How can you access a global variable inside the def TenTimesEven(VALUES):
function, if function has a variable with same name. s=0
for i in VALUES:
if i%2==0:
[15] Write the definition of a method/function s=s+(i*10)
AddOddEven(VALUES) to display sum of odd and even print("Sum:",s)
values separately from the list of VALUES. n=int(input("Enter the value:"))
l=[]
For example : for i in range(n):
v=int(input("Enter value:"))
If the VALUES contain [15, 26, 37, 10, 22, 13] l.append(v)
TenTimesEven(l)
The function should display [3] Write the definition of a method/function
EndingA(Names) to search and display those strings from
Even Sum: 58 the list of Names, which are ending with ‘A’.
For example, If the Names contain
Odd Sum: 65 [“JAYA”,”KAREEM”,”TARUNA”,”LOVISH”]
The method/function should display JAYA TARUNA
Ans.: Ans.:
def EndingA(names):
def AddOddEven(VALUES): for i in names:
esum=0 if i.endswith('a'):
osum=0 print(i,end=' ')
for i in VALUES: n=int(input("Enter the value:"))
if i%2==0: l=[]
esum+=i for i in range(n):
else: v=input("Enter Name to add:")
osum+=i l.append(v)
print("Even Sum:",esum) EndingA(l)
print("Odd Sum:",osum)
n=int(input("Enter no. of elements:")) [4] Write a python method/function Scroller(Lineup) to
l=[] scroll all the elements of a list Lineup by one element
for i in range(n): ahead and move the last element to the first. Also, display
v=int(input("Enter Value to add:")) the changed content of the list. For Example: If the list has
l.append(v) the following values in it [25,30,90,110,16]
AddOddEven(l)
After changing the list content should be displayed as
Functions Class 12 Computer Science 3 Marks Questions [16,25,30,90,110]
[1] Write a method in python to find and display the Ans.:
composite numbers between 2 to N. Pass N as an def Scroller(Lineup):
argument to the method. Lineup=Lineup[-1:]+Lineup[:-1]
Ans.: print(str(Lineup))
def dis_CompoSite(N): n=int(input("Enter the value:"))
print("Composite Numbers between 2 to N:",end='') l=[]
for i in range(2,N+1): for i in range(n):
v=int(input("Enter Name to add:")) temp = Numbers[i]
l.append(v) Numbers[i] = Numbers[i+hf+start]
Scroller(l) Numbers[i+hf+start] = temp
n=int(input("Enter no. of elements:"))
[5] Write a python method/function REVERSAR(Number) l=[]
to find a new number Reverse from Number with each of for i in range(n):
the digits of Number in reversed order and display the v=int(input("Enter value to append:"))
content of Reverse on screen. l.append(v)
For Example: swapper(l)
If the value of Number is 3451 print(l)
The method/function should be displayed as 1543 [8] Write a python method/function Count3and7(N) to
Ans.: find and display the count of all those numbers which are
def REVERSAR(number): between 1 and N, which are either divisible by 3 or by 7.
r=0 For example :
while number!=0: If the value of N is 15
r=(number%10)+(r*10) The sum should be displayed as 7 (as 3,6,7,9,12,14,15 in
number//=10 between 1 to 15 are either divisible by 3 or 7)
print("Reverse:",r) Ans.:
n=int(input("Enter the value:")) def Count3and7(N):
REVERSAR(n) c=0
[6] Write the definition of a method/function for i in range(1,N+1):
HowMany(ID,Val) to count and display number of times if i%3==0 or i%7==0:
the value of Val is present in the list ID. c+=1
For example : If the ID contains print("Numbers divisible by 3 and 7 are:",c)
[115,122,137,110,122,113] and Val contains 122 n=int(input("Enter a number:"))
The function should display 122 found 2 Times Count3and7(n)
Ans.:
def HowMany(ID,val): [9] Find the output for the following:
c=0 def Change(P ,Q=30):
for i in ID: P=P+Q
if i==val: Q=P-Q
c+=1 print( P,"#",Q)
print(val, "found ",c," time") return (P)
n=int(input("Enter total no. of values:")) R=150
s=int(input("Enter number to search:")) S=100
l=[] R=Change(R,S)
for i in range(n): print(R,"#",S)
v=int(input("Enter Number to add:")) S=Change(S)
l.append(v) Ans.;
HowMany(l,s) 250 # 150
250 # 100
[7] Write a python method/function Swapper(Numbers) 130 # 100
to swap the first half of the content of a list of Numbers
with the second half of the content of the list of Numbers [10] Write a function LShift(Arr,n) in Python, which
and display the swapped values. accepts a list Arr of numbers and n is a numeric value by
Note : Assuming that the list has even number of values in which all elements of the list are shifted to left.
it.
For example : Sample Input Data of the list Arr= [ 10,20,30,40,12,11],
If the list Numbers contains [35,67,89,23,12,45] n=2

After swapping the list content should be displayed as Output Arr = [30,40,12,11,10,20]
[23,12,45,35,67,89]
Ans.: Ans.:
def swapper(Numbers): def Lshift(Arr,n):
if len(Numbers)%2 == 0: print(Arr[n:]+Arr[:n])
start = 0 l=[]
else: m=int(input("Enter no. of elements"))
start = 1 n=int(input("Enter no. shift elements:"))
hf = len(Numbers)//2
for i in range(hf): for i in range(m):
v=int(input("Enter value to add into the list:"))
l.append(v)
Lshift(l,n)

[11] 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,6]
The indexList will have – [0,1,3,5]
Ans.:
def index_list(L):
il=[]
for i in range(len(L)):
if L[i]!=0:
il.append(i)
return il
l=[]
n=int(input("Enter no. of elements"))
for i in range(n):
v=int(input("Enter value to add into the list:"))
l.append(v)
print(index_list(l))

[12] Write the output of the following:

def Convert(X=45,Y=30) :
X=X+Y Y=X–Y
print(X,"&",Y)
return X
A=250
B=150
A=Convert(A,B)
print (A,"&",B)
B=Convert(B)
print(A,"&",B)
A=Convert(A)
print(A,"&",B)

Ans.:
400 & 250
400 & 150
180 & 150
400 & 180
430 & 400
430 & 180

You might also like