Python Questions and Answers – Function – 2

Python Multiple Choice Questions & Answers (MCQs) focuses on “Functions”.

Q 1. Which are the advantages of functions in python?
A. Reducing duplication of code
B. Decomposing complex problems into simpler pieces
C. Improving clarity of the code
D. All of the mentioned

Show Answer Answer:-D. All of the mentioned
Explanation None.


Q 2. What are the two main types of functions?
A. Custom function
B. Built-in function & User defined function
C. User function
D. System function

Show Answer Answer:-B. Built-in function & User defined function
Explanation Built-in functions and user defined ones. The built-in functions are part of the Python language. Examples are: dir(), len() or abs(). The user defined functions are functions created with the def keyword.


Q 3. Where is function defined?
A. Module
B. Class
C. Another function
D. All of the mentioned

Show Answer Answer:-D. All of the mentioned
Explanation Functions can be defined inside a module, a class or another function.


Q 4. What is called when a function is defined inside a class?
A. Module
B. Class
C. Another function
D. Method

Show Answer Answer:-D. Method
Explanation None.


Q 5. Which of the following is the use of id() function in python?
A. Id returns the identity of the object
B. Every object doesn’t have a unique id
C. All of the mentioned
D. None of the mentioned

Show Answer Answer:-A. Id returns the identity of the object
Explanation Each object in Python has a unique id. The id() function returns the object’s id.

Q 6. Which of the following refers to mathematical function?
A. sqrt
B. rhombus
C. add
D. rhombus

Show Answer Answer:-A. sqrt
Explanation Functions that are always available for usage, functions that are contained within external modules, which must be imported and functions defined by a programmer with the def keyword. Eg: math import sqrt A sqrt() function is imported from the math module.


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

def cube(x):
return x * x * x
x = cube(3)
print x
A. 9
B. 3
C. 27
D. 30

Show Answer Answer:-C. 27
Explanation A function is created to do a specific task. Often there is a result from such a task. The return keyword is used to return values from a function. A function may or may not return a value. If a function does not have a return keyword, it will send a none value.


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

def C2F(c):
return c * 9/5 + 32
print C2F(100)
print C2F(0)
A. 212
32
B. 314
24
C. 567
98
D. None of the mentioned

Show Answer Answer:-A. 212 32
Explanation The code shown above is used to convert a temperature in degree celsius to fahrenheit.


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

def power(x, y=2):
r = 1
for i in range(y):
r = r * x
return r
print power(3)
print power(3, 3)
A. 212
32
B. 9
27
C. 567
98
D. None of the mentioned

Show Answer Answer:-B. 9 27
Explanation The arguments in Python functions may have implicit values. An implicit value is used, if no value is provided. Here we created a power function. The function has one argument with an implicit value. We can call the function with one or two arguments.


Q 10. What will be the output of the following Python code?

def sum(*args):
”’Function returns the sum of all values”’
r = 0
for i in args:
r += i
return r
print(sum.__doc__)
print(sum(1, 2, 3))
print(sum(1, 2, 3, 4, 5))
A .Function returns the sum of all values
6
15
B. 6
100
C. 123
12345
D. None of the mentioned

Show Answer Answer:-A .Function returns the sum of all values 6 15
Explanation We use the * operator to indicate, that the function will accept arbitrary number of arguments. The sum() function will return the sum of all arguments. The first string in the function body is called the function documentation string. It is used to document the function. The string must be in triple quotes.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page