0% found this document useful (0 votes)
156 views

Functions

This document contains questions about Python functions including: - The difference between local and global variables - Using the global keyword in a function with an example - The difference between formal and actual parameters in a function call - Examples of function definitions and calls with explanations of output - Questions about scope of variables in functions

Uploaded by

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

Functions

This document contains questions about Python functions including: - The difference between local and global variables - Using the global keyword in a function with an example - The difference between formal and actual parameters in a function call - Examples of function definitions and calls with explanations of output - Questions about scope of variables in functions

Uploaded by

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

1 What is difference between local variable and global variable

2 Explain the use of global keyword used in function with the help of a
suitable example

3 State True or False

In Python function can return only one value-


What is the difference between the formal parameter and actual
parameters?

Ans : Actual parameter is a parameter which is used in function call


statement to send a value from calling function to the called function. It is
also known as Argument.
Formal parameter is a parameter which is used in function header of the
called function to receive the values from actual parameter. It is also
known as Parameter .for example

def example(x,y,z): #Called Function (x,y,z Formal parameter)


print( x+y+z)

example(a,b,c) # Calling Function(main) a,b,c Actual Parameter

Write output of following code Function


9

For a function header 1


def Calc (X ,Y=20):

Which of the following function call will give an error?


a) Calc (15, 25) b) Calc (X=15,Y=25)
c) Calc (Y=25) d) Calc (X=25)

Which of the following function header is correct? 1


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)

What will be the output of the following Python code? 1


def add (num1, num2):
sum = num1 + num2
sum = add(20,30)
print(sum)

a. 50 b. 0 c. Null d. None
What will be the output of the following code? 1
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
What will be the output of the following code? 1

a. 50#50 b. 50#5 c. 50#30 d. 5#50#


Which of the following is not correct in context of scope of variable? 1
a) global keyword is used to change value of a global variable in a local
scope
b) local keyword is used to change value of a local variable in a global
scope
c) global variable can be accessed without using the global keyword in a
local scope
d) local variable cannot be used outside its scope.

Write the output of the following Python code : 1


def Update(X=10):
X += 15
print('X = ', X)
X=20
Update()
print('X = ', X)
What will be the output of the following code? 1
x=3
def myfunc():
global x
x+=2
print(x, end=' ')
print(x, end=' ')
myfunc()
print(x, end=' ')
Write the output of the following code : 2
g=0
def fun1(x, y):
global g
g=x+y
print(g)
return g
def fun2(m,n):
global g
g=m-n
return g
k=fun1(2,3)
print("k=",k)
fun2(k,7)
print(g)

What possible output(s) are expected to be displayed on screen at the 2


time of execution of the following code ?
import random
S=["Pen","Pencil","Eraser","Bag","Book"]
for i in range (1,2):
f=random.randint(i,3)
s=random.randint(i+1,4)
print( S[f], S[s], sep=":")

Options :
(I) Pencil:Book (II) Pencil:Book
Eraser:Bag
(III) Pen:Book (IV) Bag:Eraser
Bag:Book

Write the output of the Python code given below 3


def change(P,Q=30):
P=P+Q
Q=P-Q
print(P,"#",Q)
return P
R=150
S=100
R=change(R,S)
print(R,"#",S)
S=change(S)
print(R,"#",S)
Write the output of the Python code given below : 2
a=15
def update(x):
global a
a+=2
if x%2==0:
a*=x
else:
a//=x
a=a+5
print(a,end="$")
update(5)
print(a)
Predict the output of the code given below : 2

text="LearningCS"
L=len(text)
ntext=""
for i in range (0,L):
if text[i].islower():
ntext=ntext+text[i].upper()
elif text [i].isalnum():
ntext=ntext+text[i 1]
else:
ntext=ntext+'&&'
print(ntext)
3

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=' ')

(a) Write the output of the code given below:


(a) Predict the output of the code given below: 3

What will be the output of the following code?


import random
List=["Delhi","Mumbai","Chennai","Kolkata"]
for y in range(4):
x = random.randint(1,3)
print(List[x],end="#")

a. Delhi#Mumbai#Chennai#Kolkata#
b. Mumbai#Chennai#Kolkata#Mumbai#
c. Mumbai# Mumbai #Mumbai # Delhi#
d. Mumbai# Mumbai #Chennai # Mumbai
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# 40
Find the output of the following code:
Name="PythoN3.1"
R=""
for x in range(len(Name)):
if Name[x].isupper():
R=R+Name[x].lower()
elif Name[x].islower():
R=R+Name[x].upper()
elif Name[x].isdigit():
R=R+Name[x-1]
else: R=R+"#" print(R)
a. pYTHOn##@
b. pYTHOnN#@
c. pYTHOn#@
d. pYTHOnN@#
What will be the output of the following python code?
S=”Welcome”
def Change(T):
T=”HELLO”
print(T,end=’@’)
Change(S)
Print(s)

a) WELCOME@HELLO b)HELLO@HELLO
c)HELLO@WELCOME d)WELCOME@WELCOME
Identify the Correct possible output for the following Python Code:
import random
for N in range(2,5,2):
print(random.randrange(1,N),end=’#’)
a) 1#3#5# b)2#3#
c) 1#4# d) 1#3#

What will be the output of the following Python code?


def FunStr(S):
T=""
for i in S:
if i.isdigit():
T=T+ i
return T
X="PYTHON 3.9"
Y=FunStr(X)
print(X,Y,sep="*")
a)PYTHON 3.9 b)PYTHON 3.9*3.9

c) PYTHON 3.9*39 d)ERROR

What will be the output of the following Python code?

a) 20 # 50 @ 20 b) 50 @ 20 # 50
c) 50 # 50 # 50 d) 20 @ 50 # 20

What is the output of the following Python code?

a) 4@ 12@ 27@ 20@ b) 6 @18 @27@ 50@


c) 20 @36@ 27@ 100@ d) Error

a) Explain the use of positional parameters in a Python function with the help of a
suitable example.
(b) Explain the use of a default parameter in a Python function with the help of a
suitable example.

Write the output for the execution of the following Python code :
Write the definition of a function Sum3(L) in Python, which accepts a list L of integers and
displays the sum of all such integers from the list L which end with the digit 3.
For example, if the list L is passed
[ 123, 10, 13, 15, 23]
then the function should display the sum of 123, 13, 23, i.e. 159 as follows :
Sum of integers ending with digit 3 = 159

You might also like