Python Questions and Answers – Function – 2

This set of Python Questions for entrance examinations focuses on “Functions”.

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

Answer: d
Explanation: None.

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

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

3. Where is function defined?
a) Module
b) Class
c) Another function
d) All of the mentioned
View Answer

Answer: d
Explanation: Functions can be defined inside a module, a class or another function.
advertisement
advertisement

4. What is called when a function is defined inside a class?
a) Module
b) Class
c) Another function
d) Method
View Answer

Answer: d
Explanation: None.

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

Answer: a
Explanation: Each object in Python has a unique id. The id() function returns the object’s id.

6. Which of the following refers to mathematical function?
a) sqrt
b) rhombus
c) add
d) rhombus
View Answer

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

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

advertisement
  1. def cube(x):
  2.     return x * x * x      
  3. x = cube(3)    
  4. print x

a) 9
b) 3
c) 27
d) 30
View Answer

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

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

  1. def C2F(c):
  2.     return c * 9/5 + 32
  3. print C2F(100)
  4. print C2F(0)

a)

212
32

b)

314
24

c)

567
98

d) None of the mentioned
View Answer

Answer: a
Explanation: The code shown above is used to convert a temperature in degree celsius to fahrenheit.

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

  1. def power(x, y=2):
  2.     r = 1
  3.     for i in range(y):
  4.        r = r * x
  5.     return r
  6. print power(3)
  7. print power(3, 3)

a)

212
32

b)

9
27

c)

567
98

d) None of the mentioned
View Answer

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

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

  1. def sum(*args):
  2.    '''Function returns the sum 
  3.    of all values'''
  4.    r = 0
  5.    for i in args:
  6.       r += i
  7.    return r
  8. print sum.__doc__
  9. print sum(1, 2, 3)
  10. print sum(1, 2, 3, 4, 5)

a)

6
15

b)

6
100

c)

123
12345

d) None of the mentioned
View Answer

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

Sanfoundry Global Education & Learning Series – Python.

To practice all extrance examination questions on 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.