0% found this document useful (0 votes)
604 views

MCQ Function

The document contains 10 multiple choice questions about Python functions. It covers topics like defining functions, function parameters, return values, built-in vs user-defined functions, and default arguments. The questions test understanding of basic function concepts in Python like defining functions with def, calling functions, passing arguments, returning values, scope of variables, and using functions like math.sqrt() and id().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
604 views

MCQ Function

The document contains 10 multiple choice questions about Python functions. It covers topics like defining functions, function parameters, return values, built-in vs user-defined functions, and default arguments. The questions test understanding of basic function concepts in Python like defining functions with def, calling functions, passing arguments, returning values, scope of variables, and using functions like math.sqrt() and id().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Python Questions and Answers – Function – 1

Multiple Choice Questions & Answers (MCQs) :

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

Answer: c
Explanation: None.
3. What will be the output of the following Python code?
advertisement
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
4. 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

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?
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

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?
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

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?
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
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?
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

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?
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

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

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.

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

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

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

Answer: d
Explanation: Functions can be defined inside a module, a class or another function.
advertisement
4. What is called when a function is defined inside a class?
a) Module
b) Class
c) Another function
d) Method

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

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

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?
def cube(x):
return x * x * x
x = cube(3)
print x
a) 9
b) 3
c) 27
d) 30

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

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

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?
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)
6
15
b)
6
100
c)
123
12345
d) None of the mentioned
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.

Python Questions and Answers – Function – 3

This set of Python Questions for campus interview focuses on “Functions”.


1. Python supports the creation of anonymous functions at runtime, using a
construct called __________
a) lambda
b) pi
c) anonymous
d) none of the mentioned

Answer: a
Explanation: Python supports the creation of anonymous functions (i.e. functions
that are not bound to a name) at runtime, using a construct called lambda. Lambda
functions are restricted to a single expression. They can be used wherever normal
functions can be used.
2. What will be the output of the following Python code?
y=6
z = lambda x: x * y
print z(8)
a) 48
b) 14
c) 64
d) None of the mentioned

Answer: a
Explanation: The lambda keyword creates an anonymous function. The x is a
parameter, that is passed to the lambda function. The parameter is followed by a
colon character. The code next to the colon is the expression that is executed, when
the lambda function is called. The lambda function is assigned to the z variable.
The lambda function is executed. The number 8 is passed to the anonymous
function and it returns 48 as the result. Note that z is not a name for this function. It
is only a variable to which the anonymous function was assigned.
advertisement
3. What will be the output of the following Python code?
lamb = lambda x: x ** 3
print(lamb(5))
a) 15
b) 555
c) 125
d) None of the mentioned

Answer: c
Explanation: None.
4. Does Lambda contains return statements?
a) True
b) False

Answer: b
Explanation: lambda definition does not include a return statement. it always
contains an expression which is returned. Also note that we can put a lambda
definition anywhere a function is expected. We don’t have to assign it to a variable
at all.
5. Lambda is a statement.
a) True
b) False

Answer: b
Explanation: lambda is an anonymous function in Python. Hence this statement is
false.
6. Lambda contains block of statements.
a) True
b) False

Answer: b
Explanation: None.
7. What will be the output of the following Python code?
def f(x, y, z): return x + y + z
f(2, 30, 400)

a) 432
b) 24000
c) 430
d) No output

Answer: a
Explanation: None.

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


def writer():
title = 'Sir'
name = (lambda x:title + ' ' + x)
return name
 
who = writer()
who('Arthur')
a) Arthur Sir
b) Sir Arthur
c) Arthur
d) None of the mentioned

Answer: b
Explanation: None.
9. What will be the output of the following Python code?
L = [lambda x: x ** 2,
lambda x: x ** 3,
lambda x: x ** 4]
 
for f in L:
print(f(3))
a)
27
81
343

b)
6
9
12

c)
9
27
81

d) None of the mentioned

Answer: c
Explanation: None.
10. What will be the output of the following Python code?
min = (lambda x, y: x if x < y else y)
min(101*99, 102*98)

a) 9997
b) 9999
c) 9996
d) None of the mentioned

Answer: c
Explanation: None.

Python Questions and Answers – Function – 4

1. What is a variable defined outside a function referred to as?


a) A static variable
b) A global variable
c) A local variable
d) An automatic variable

Answer: b
Explanation: The value of a variable defined outside all function definitions is
referred to as a global variable and can be used by multiple functions of the
program.
2. What is a variable defined inside a function referred to as?
a) A global variable
b) A volatile variable
c) A local variable
d) An automatic variable

Answer: c
Explanation: The variable inside a function is called as local variable and the variable
definition is confined only to that function.
3. What will be the output of the following Python code?
advertisement
i=0
def change(i):
i=i+1
return i
change(1)
print(i)
a) 1
b) Nothing is displayed
c) 0
d) An exception is thrown

Answer: c
Explanation: Any change made in to an immutable data type in a function isn’t
reflected outside the function.
4. What will be the output of the following Python code?
def a(b):
b = b + [5]
 
c = [1, 2, 3, 4]
a(c)
print(len(c))
a) 4
b) 5
c) 1
d) An exception is thrown

Answer: b
Explanation: Since a list is mutable, any change made in the list in the function is
reflected outside the function.
5. What will be the output of the following Python code?
a=10
b=20
def change():
global b
a=45
b=56
change()
print(a)
print(b)
a)
10
56
b)
45
56
c)
10
20
d) Syntax Error

Answer: a
Explanation: The statement “global b” allows the global value of b to be accessed
and changed. Whereas the variable a is local and hence the change isn’t reflected
outside the function.
6. What will be the output of the following Python code?
def change(i = 1, j = 2):
i=i+j
j=j+1
print(i, j)
change(j = 1, i = 2)
a) An exception is thrown because of conflicting values
b) 1 2
c) 3 3
d) 3 2

Answer: d
Explanation: The values given during function call is taken into consideration, that is,
i=2 and j=1.
7. What will be the output of the following Python code?
def change(one, *two):
print(type(two))
change(1,2,3,4)
a) Integer
b) Tuple
c) Dictionary
d) An exception is thrown

Answer: b
Explanation: The parameter two is a variable parameter and consists of (2,3,4).
Hence the data type is tuple.
8. If a function doesn’t have a return statement, which of the following does the
function return?
a) int
b) null
c) None
d) An exception is thrown without the return statement

Answer: c
Explanation: A function can exist without a return statement and returns None if the
function doesn’t have a return statement.
9. What will be the output of the following Python code?
def display(b, n):
while n > 0:
print(b,end="")
n=n-1
display('z',3)
a) zzz
b) zz
c) An exception is executed
d) Infinite loop

Answer: a
Explanation: The loop runs three times and ‘z’ is printed each time.
10. What will be the output of the following Python code?
def find(a, **b):
print(type(b))
find('letters',A='1',B='2')
a) String
b) Tuple
c) Dictionary
d) An exception is thrown

Answer: c
Explanation: b combines the remaining parameters into a dictionary.

You might also like