1.
Which of the following is the use of function in python?
a) Functions are reusable pieces of programs
b) Functions don’t provide better modularity for your application
c) you can’t also create your own functions
d) All of the mentioned
Answer: a
EXPLANATION: Functions are reusable pieces of programs. They allow you to give a name to a block of statements, allowing you to run that block using the specified name anywhere in your program and any number of times.
2.
Which keyword is use for function?
a) fun
b) define
c) def
d) function
Answer: c
3.
What is the output of the below program?
def printMax(a, b):
if a > b:
print(a, ‘is maximum’)
elif a == b:
print(a, ‘is equal to’, b)
else:
print(b, ‘is maximum’)
printMax(3, 4)
a) 3
b) 4
c) 4 is maximum
d) None of the mentioned
Answer: c
EXPLANATION: Here, we define a function called printMax that uses two parameters called a and b. We find out the greater number using a simple if..else statement and then print the bigger number.
4.
What is the output of the below program?
def sayHello():
print('Hello World!')
sayHello()
sayHello()
a)
Hello World!
Hello World!
b)
‘Hello World!’
‘Hello World!’
c)
Hello
Hello
d) None of the mentioned
Answer: a
EXPLANATION: Functions are defined using the def keyword. After this keyword comes an identifier name for the function, followed by a pair of parentheses which may enclose some names of variables, and by the final colon that ends the line. Next follows the block of statements that are part of this function.
def sayHello():
print(‘Hello World!’) # block belonging to the function
# End of function #
sayHello() # call the function
sayHello() # call the function again
5.
What is the output of the below program ?
x = 50
def func(x):
#print(‘x is’, x)
x = 2
#print(‘Changed local x to’, x)
func(x)
print(‘x is now’, x)
a) x is now 50
b) x is now 2
c) x is now 100
d) None of the mentioned
Answer: a
EXPLANATION: The first time that we print the value of the name x with the first line in the function’s body, Python uses the value of the parameter declared in the main block, above the function definition.
Next, we assign the value 2 to x. The name x is local to our function. So, when we change the value of x in the function, the x defined in the main block remains unaffected.
With the last print function call, we display the value of x as defined in the main block, thereby confirming that it is actually unaffected by the local assignment within the previously called function.