Working with functions - Worksheet 1
Working with functions - Worksheet 1
GERUGAMBAKKAM
CHENNAI
Worksheet – Working with Functions
1) What will be the output of the following Python code?
x = 50
def func(x):
print('x is', x)
x=2
print('Changed local x to', x)
func(x)
print('x is now', x)
5. Observe the following Python code very carefully and rewrite it after removing
all syntactical errors with each correction underlined.
DEF result_even( ):
x = input(“Enter a number”)
if (x % 2 = 0) :
print (“You entered an even number”)
else:
print(“Number is odd”)
Even ( )
count = 0
for i in mystr:
if count%2 !=0:
newstr = newstr+str(count)
else:
if i.islower():
newstr = newstr+i.upper()
else:
newstr = newstr+i
count +=1
newstr = newstr+mystr[:1]
print("The new string is :", newstr)
makenew("sTUdeNT")
12.Write a Flow of execution of the following code? Also find the output
def Call(P=40,Q=20):
P=P+Q
Q=P-Q
print(P,'@',Q)
return P
R=200
S=100
R=Call(R,S)
print (R,'@',S)
S=Call(S)
print(R,'@',S)
13.Find Error of the following code.
def calc(u):
if u%2==0
return u+10;
else:
return u+2
def pattern(M,B=2):
for CNT in range(0,B):
print(calc(CNT), M, end=””);
pattern(*)
pattern(“#”,4)
pattern(@.3)
14. Write a statement to call the function.
def Add():
X = 10 + 20
print(X)
________ #statement to call the above function
15.Write a statement to call the function.
def Add(X,Y):
Z = X+Y
print(Z)
________ #statement to call the above function
16.Write a statement to call the function.
def Add(X,Y):
Z = X+Y
return Z
________# statement to call the above function
print(“Total =”,C)
17.Which Line Number Code will never execute?
def Check(num): #Line 1
if num%2==0: #Line 2
print("Hello") #Line 3
return True #Line 4
print("Bye") #Line 5
else: #Line 6
return False #Line 7
C = Check(20) #Line 8
print(C) #Line 9
18.Call the given function using KEYWORD ARGUMENT with values 100 and 200.
Def Swap(num1,num2):
num1,num2=num2,num1
print(num1,num2)
Swap(num1=100,num2=200)
19. What will be the output of the following code?
def drawline(char='$',time=5):
print(char*time)
drawline()
drawline('@',10)
drawline(65)
drawline(chr(65))
20.Which of the following function headers is correct?
(a) def myFunc(x = 2, y = 3, z):
(b) def myFunc(x = 2, y, z = 3):
(c) def myFunc(x, y = 2, z = 3):
(d) def myFunc(x, y, z = 3, p):
21. If the return statement is not used in the function then which type of value will be
returned by the function?
a) int b) str c) float d) None
22. Divya wants to print the identity of the object used in the function. Which of
the following functions is used to print the same?
a) identity() b) ide() c) id() d) idy()
23. The default value for a parameter is defined in function ?
a. Definition b. Return statement
c. Header d. None of the above
24. Function randint() is available under which module of python?
a. random b. math c. statistics d. None
25. By default if you return multiple value separated by comma, then it is
returned as tuple__
26. In arguments we can skip the default argument, but all the keyword
arguments should match the parameters in the function definitions.
27. ________ keyword is used to define a function.
28. Function will perform its action only when it is ________.
29. A python function may return multiple values?
a. True b. False
30.A python program execution begins with first statement of _main_ segment?
a. True b. False
Please choose the correct choice out of the four options given below:
a. Both Assertion and reason are true and reason is the correct explanation of
assertion.
b. Assertion and reason both are true but reason is not the correct explanation of
assertion.
c. Assertion is true, reason is false.
d. Assertion is false, reason is true
1. Assertion: The function header ‘def read (a=2, b=5, c):’ is not correct.
Reason: Non default arguments can’t follow default arguments.
2. Assertion: Every function returns a value if the function does not explicitly return a
value, then it will return ‘Zero’.
Reason: Zero is not equivalent to None.
3. Consider the following code:
x = 100
def study( ):
global x
x = 50
print(x)
Assertion: 50 will be the output of above code.
Reason: Because the x used inside the function study( ) is of local scope.
4. Assertion (A): Keyword arguments are related to the function calls
Reason (R): When you use keyword arguments in a function call, the caller
identify
the arguments by the parameter name.
5. Assertion (A): global keyword is used inside a function to enable the function
alter the value of a global variable.
Reasoning (R): A global variable cannot be accessed by a function without the
use of global keyword.
6. Study the following program and select the possible output(s) and write maximum
and minimum value assigned to the variable y .
import random
x=random.random( )
y=random.randint(0,4)
print(int(x),”:”,y+int(x))
(i) 0:0 (ii) 1: 6 (iii) 2:4 (iv) 0:3
7. What possible output(s) are expected to be displayed on screen at the time of
execution of the following code? Also specify the maximum and minimum value
that can be assigned to variable X.
import random
L=[10,7,21]
X=random.randint(1,2)
for i in range(X):
Y=random.randint(1,X)
print(L[Y],”$”,end=” ”)
(a) 10 $ 7 $ (b) 21 $ 7 $ (c) 21 $ 10 $ (d) 7 $ 37
Maximum value that can be assigned to variable X is 2
Minimum value that can be assigned to variable X is 1