Python MCQ’s on Function: SET 4

31. 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

Check Answer

Answer: a
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.

32. Which keyword is used for function?

a) Fun

b) Define

c) Def

d) Function

Check Answer

Answer: c
None.

33. What will be the output of the following Python code?

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

Check Answer

Answer: a
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

34. What will be the output of the following Python code?

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

Check Answer

Answer: c
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.

35. 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)

a)

x is 50
Changed local x to 2
x is now 50

b)

x is 50
Changed local x to 2
x is now 2

c)

x is 50
Changed local x to 2
x is now 100

d) None of the mentioned

Check Answer

Answer: a
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.

36. What will be the output of the following Python code?

x = 50

def func():

global x

print(‘x is’, x)

x = 2

print(‘Changed global x to’, x)

func()

print(‘Value of x is’, x)

a)

x is 50
Changed global x to 2
Value of x is 50

b)

x is 50
Changed global x to 2
Value of x is 2

c)

x is 50
Changed global x to 50
Value of x is 50

d) None of the mentioned

Check Answer

Answer: b
The global statement is used to declare that x is a global variable – hence, when we assign a value to x inside the function, that change is reflected when we use the value of x in the main block.

37. What will be the output of the following Python code?

def say(message, times = 1):

print(message * times)

say(‘Hello’)

say(‘World’, 5)

a)

Hello
WorldWorldWorldWorldWorld

b)

Hello
World 5

c)

Hello
World,World,World,World,World

d)

Hello
HelloHelloHelloHelloHello

Check Answer

Answer: a
For some functions, you may want to make some parameters optional and use default values in case the user does not want to provide values for them. This is done with the help of default argument values. You can specify default argument values for parameters by appending to the parameter name in the function definition the assignment operator (=) followed by the default value.
The function named say is used to print a string as many times as specified. If we don’t supply a value, then by default, the string is printed just once. We achieve this by specifying a default argument value of 1 to the parameter times.
In the first usage of say, we supply only the string and it prints the string once. In the second usage of say, we supply both the string and an argument 5 stating that we want to say the string message 5 times.

38. What will be the output of the following Python code?

def func(a, b=5, c=10):

print(‘a is’, a, ‘and b is’, b, ‘and c is’, c)

func(3, 7)

func(25, c = 24)

func(c = 50, a = 100)

a)

a is 7 and b is 3 and c is 10
a is 25 and b is 5 and c is 24
a is 5 and b is 100 and c is 50

b)

a is 3 and b is 7 and c is 10
a is 5 and b is 25 and c is 24
a is 50 and b is 100 and c is 5

c)

a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50

d) None of the mentioned

Check Answer

Answer: c
If you have some functions with many parameters and you want to specify only some of them, then you can give values for such parameters by naming them – this is called keyword arguments – we use the name (keyword) instead of the position (which we have been using all along) to specify the arguments to the function.
The function named func has one parameter without a default argument value, followed by two parameters with default argument values.
In the first usage, func(3, 7), the parameter a gets the value 3, the parameter b gets the value 7 and c gets the default value of 10.

In the second usage func(25, c=24), the variable a gets the value of 25 due to the position of the argument. Then, the parameter c gets the value of 24 due to naming i.e. keyword arguments. The variable b gets the default value of 5.

In the third usage func(c=50, a=100), we use keyword arguments for all specified values. Notice that we are specifying the value for parameter c before that for a even though a is defined before c in the function definition.

39. What will be the output of the following Python code?

def maximum(x, y):

if x > y:

return x

elif x == y:

return ‘The numbers are equal’

else:

return y

print(maximum(2, 3))

a) 2

b) 3

c) The numbers are equal

d) None of the mentioned

Check Answer

Answer: b
The maximum function returns the maximum of the parameters, in this case the numbers supplied to the function. It uses a simple if..else statement to find the greater value and then returns that value.

40. Which of the following is a feature of DocString?

a) Provide a convenient way of associating documentation with Python modules, functions, classes, and methods

b) All functions should have a docstring

c) Docstrings can be accessed by the __doc__ attribute on objects

d) All of the mentioned

Check Answer

Answer: d
Python has a nifty feature called documentation strings, usually referred to by its shorter name docstrings. DocStrings are an important tool that you should make use of since it helps to document the program better and makes it easier to understand.

Page  1  2  3  4  5  6  7

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

Leave a Comment