Python Multiple Choice Questions – Function

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Function”.

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
View Answer

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 used for function?
a) Fun
b) Define
c) def
d) Function
View Answer

Answer: c
Explanation: The def keyword is used to create, (or define) a function in python.

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

advertisement
advertisement
  1. def sayHello():
  2.     print('Hello World!') 
  3. sayHello() 
  4. sayHello()

a)

Hello World!
Hello World!
Note: Join free Sanfoundry classes at Telegram or Youtube

b)

'Hello World!'
'Hello World!'

c)

Hello
Hello
advertisement

d) None of the mentioned
View Answer

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.

  1. def sayHello():
  2.     print('Hello World!') # block belonging to the function
  3. # End of function #
  4.  
  5. sayHello() # call the function
  6. sayHello() # call the function again

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

advertisement
  1. def printMax(a, b):
  2.     if a > b:
  3.         print(a, 'is maximum')
  4.     elif a == b:
  5.         print(a, 'is equal to', b)
  6.     else:
  7.         print(b, 'is maximum')
  8. printMax(3, 4)

a) 3
b) 4
c) 4 is maximum
d) None of the mentioned
View Answer

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.

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

  1. x = 50
  2. def func(x):
  3.     print('x is', x)
  4.     x = 2
  5.     print('Changed local x to', x)
  6. func(x)
  7. 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
View Answer

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.

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

  1. x = 50
  2. def func():
  3.     global x
  4.     print('x is', x)
  5.     x = 2
  6.     print('Changed global x to', x)
  7. func()
  8. 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
View Answer

Answer: b
Explanation: 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.

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

  1. def say(message, times = 1):
  2.     print(message * times)
  3. say('Hello')
  4. say('World', 5)

a)

Hello
WorldWorldWorldWorldWorld

b)

Hello
World 5

c)

Hello
World,World,World,World,World

d)

Hello
HelloHelloHelloHelloHello
View Answer
Answer: a
Explanation: 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.
 
 

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

  1. def func(a, b=5, c=10):
  2.     print('a is', a, 'and b is', b, 'and c is', c)
  3.  
  4. func(3, 7)
  5. func(25, c = 24)
  6. 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
View Answer

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

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

  1. def maximum(x, y):
  2.     if x > y:
  3.         return x
  4.     elif x == y:
  5.         return 'The numbers are equal'
  6.     else:
  7.         return y
  8.  
  9. print(maximum(2, 3))

a) 2
b) 3
c) The numbers are equal
d) None of the mentioned
View Answer

Answer: b
Explanation: 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.

10. 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
View Answer

Answer: d
Explanation: 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.

More MCQs on Python Function:

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.