CBSE CS

## Functions in Python

### 100 +MCQ Practice Questions

1. Which keyword is use for function?

A. define
B. fun
C. def
D. function

#Ans : C Explanation: Keyword def marks the start of function header. So, option C is correct.

2. Which of the following items are present in the function header?

A. function name
B. parameter list
C. return value
D. Both A and B

#Ans : D Explanation: function header consists of function name and parameter list. So, option D is correct.

3. What is called when a function is defined inside a class?

A. class
B. function
C. method
D. module

#Ans : C Explanation: method is called when a function is defined inside a class. So, option C is correct.

4. If return statement is not used inside the function, the function will return:

A. None
B. 0
C. Null
D. Arbitary value

#Ans : A Explanation: If return statement is not used inside the function, the function will return None. So, option A is correct.

5. What is a recursive function?

A. A function that calls other function.
B. A function which calls itself.
C. Both A and B
D. None of the above

#Ans : B Explanation: Recursive functions are the functions which calls itself. So, option B is correct.

6. Which of the following is the use of id() function in python?

A. Id() returns the size of object.
B. Id() returns the identity of the object.
C. Both A and B
D. None of the above

#Ans : B Explanation: The id() function returns the identity of the object. This is an integer that is unique for the given object and remains constant during its lifetime. So, option B is correct.

7. Which of the following function headers is correct?

A. def fun(a = 2, b = 3, c)
B. def fun(a = 2, b, c = 3)
C. def fun(a, b = 2, c = 3)
D. def fun(a, b, c = 3, d)

#Ans : C Explanation: All required parameters must be placed before any default arguments. Simply because they are mandatory, whereas default arguments are not. Syntactically, it would be impossible for the interpreter to decide which values match which arguments if mixed modes were allowed. A SyntaxError is raised if the arguments are not given in the correct order. So, option C is correct.

8. In which part of memory does the system stores the parameter and local variables of funtion call?

A. heap
B. stack
C. Uninitialized data segment
D. None of the above

#Ans : B Explanation: Stack, where variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the callerâ€™s environment, such as some of the machine registers, are saved on the stack. So, option B is correct.

9. How is a function declared in Python?

A. def function function_name():
B. declare function function_name():
C. def function_name():
D. declare function_name():

#Ans : C Explanation: By using def function_name(): we can declared in Python. So option C is correct.

10. Which one of the following is the correct way of calling a function?

A. function_name()
B. call function_name()
C. ret function_name()
D. function function_name()

#Ans : A Explanation: By using function_name() we can call a function in Python. So option A is correct.

11. You can also create your own functions, these functions are called?

A. built-in functions
B. user-defined functions
C. py function
D. None of the above

#Ans : B Explanation: Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.

12. Function blocks begin with the keyword?

A. define
B. fun
C. function
D. def

#Ans : D Explanation: Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).

13. The code block within every function starts with a ?

A. ;
B. ::
C. :
D. %

#Ans : C Explanation: The code block within every function starts with a colon (:) and is indented.

14. A return statement with _____ arguments.

A. No
B. 1
C. 2
D. Any

#Ans : A Explanation: The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

15. ___________ are the arguments passed to a function in correct positional order.

A. Required arguments
B. Keyword arguments
C. Default arguments
D. Variable-length arguments

#Ans : A Explanation: Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition.

16. Which of the following will print the pi value defined in math module?

A. print(pi)
B. print(math.pi)
C. from math import pi
print(pi)
D. from math import pi
print(math.pi)

#Ans : C Explanation: from math import pi, print(pi) will print the pi value defined in math module. So, option C is correct.

17. Which operator is used in Python to import modules from packages?

A. .
B. *
C. ->
D. &

#Ans : A Explanation: . operator is used in Python to import modules from packages. So, option A is correct.

18. Where is funcstion defined?

A. Module
B. class
C. Another Function
D. All of the above

#Ans : D Explanation: Function can be defined in module, class and another function. So, option D is correct.

19. Lambda is a function in python?

A. True
B. False
C. Lambda is a function in python but user can not use it.
D. None of the above

#Ans : A Explanation: lambda is an anonymous function in Python. So, option A is correct.

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

A. local variable
B. global variable
C. static Variable
D. automatic variable

#Ans : B Explanation: Variable defined outside a function is referred as Global variable and can be accessible by other functions. So, option B is correct.

21. What is the output of the following program?

z = lambda x : x * x

print(z(6))
A. 6
B. 36
C. 0
D. error

#Ans : B Explanation: This lambda function will calculate the square of a number. So, option B is correct.

22. What is the output of the following program?

print(chr(ord(chr(97))))
A. a
B. A
C. 97
D. error

#Ans : A Explanation: Outer chr and ord will cancel out and we will be left with chr(97) which will return "a". So, option A is correct.

23. Choose the correct option with reference to below Python code?

def fn(a):

print(a)

x=90

fn(x)

A. x is the formal argument.
B. a is the actual argument.
C. fn(x) is the function signature.
D. x is the actual argument.

#Ans : D Explanation: x is the actual argument that is the correct option with reference to below Python code. So option D is correct.

24. Which one of the following is incorrect?

A. The variables used inside function are called local variables.
B. The local variables of a particular function can be used inside other functions, but these cannot be used in global space
C. The variables used outside function are called global variables
D. In order to change the value of global variable inside function, keyword global is used.

#Ans : B Explanation: The local variables of a particular function can be used inside other functions, but these cannot be used in global space is one of the following is incorrect. So the option B is correct.

## MCQ ON FUNCTIONS IN PYTHONSET 2

Question 1. Which keyword is use for function?
A. define
B. fun
C. def
D. function

#Answer Ans : C

Explanation: Keyword def marks the start of function header. So, option C is correct.

Question 2. Which of the following items are present in the function header?
A. function name
B. parameter list
C. return value
D. Both A and B

#Ans : D Explanation: function header consists of function name and parameter list. So, option D is correct.

Question 3. What is called when a function is defined inside a class?
A. class
B. function
C. method
D. module

#Ans : C Explanation: method is called when a function is defined inside a class. So, option C is correct.

Question 4. If return statement is not used inside the function, the function will return:
A. None
B. 0
C. Null
D. Arbitary value

#Ans : A Explanation: If return statement is not used inside the function, the function will return None. So, option A is correct.

Question 5. What is a recursive function?
A. A function that calls other function.
B. A function which calls itself.
C. Both A and B
D. None of the above

#Ans : B Explanation: Recursive functions are the functions which calls itself. So, option B is correct.

Question 6. Which of the following is the use of id() function in python?
A. Id() returns the size of object.
B. Id() returns the identity of the object.
C. Both A and B
D. None of the above

#Ans : B Explanation: The id() function returns the identity of the object. This is an integer that is unique for the given object and remains constant during its lifetime. So, option B is correct.

Question 7. Which of the following function headers is correct?
A. def fun(a = 2, b = 3, c)
B. def fun(a = 2, b, c = 3)
C. def fun(a, b = 2, c = 3)
D. def fun(a, b, c = 3, d)

#Ans : C Explanation: All required parameters must be placed before any default arguments. Simply because they are mandatory, whereas default arguments are not. Syntactically, it would be impossible for the interpreter to decide which values match which arguments if mixed modes were allowed. A SyntaxError is raised if the arguments are not given in the correct order. So, option C is correct.

8. In which part of memory does the system stores the parameter and local variables of funtion call?

A. heap
B. stack
C. Uninitialized data segment
D. None of the above

#Ans : B Explanation: Stack, where variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the callerâ€™s environment, such as some of the machine registers, are saved on the stack. So, option B is correct.

Question 9. How is a function declared in Python?

A. def function function_name():
B. declare function function_name():
C. def function_name():
D. declare function_name():

#Ans : C Explanation: By using def function_name(): we can declared in Python. So option C is correct.

Question 10. Which one of the following is the correct way of calling a function?

A. function_name()
B. call function_name()
C. ret function_name()
D. function function_name()

#Ans : A Explanation: By using function_name() we can call a function in Python. So option A is correct.

Question 11. You can also create your own functions, these functions are called?

A. built-in functions
B. user-defined functions
C. py function
D. None of the above

#Ans : B Explanation: Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.

Question 12. Function blocks begin with the keyword?

A. define
B. fun
C. function
D. def

#Ans : D Explanation: Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).

Question 13. The code block within every function starts with a ?

A. ;
B. ::
C. :
D. %

#Ans : C Explanation: The code block within every function starts with a colon (:) and is indented.

Question 14. A return statement with _______ arguments is the same as return None.

A. No
B. 1
C. 2
D. Any

#Ans : A Explanation: The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

Question 15. ___________ are the arguments passed to a function in correct positional order.

A. Required arguments
B. Keyword arguments
C. Default arguments
D. Variable-length arguments

#Ans : A Explanation: Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition.

Question 16. Which of the following will print the pi value defined in math module?

A. print(pi)
B. print(math.pi)
C. from math import pi
print(pi)
D. from math import pi
print(math.pi)

#Ans : C Explanation: from math import pi, print(pi) will print the pi value defined in math module. So, option C is correct.

Question 17. Which operator is used in Python to import modules from packages?

A. .
B. *
C. ->
D. &

#Ans : A Explanation: . operator is used in Python to import modules from packages. So, option A is correct.

Question 18. Where is function defined?

A. Module
B. class
C. Another Function
D. All of the above

#Ans : D Explanation: Function can be defined in module, class and another function. So, option D is correct.

Question 19. Lambda is a function in python?

A. True
B. False
C. Lambda is a function in python but user can not use it.
D. None of the above

#Ans : A Explanation: lambda is an anonymous function in Python. So, option A is correct.

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

A. local variable
B. global variable
C. static Variable
D. automatic variable

#Ans : B Explanation: Variable defined outside a function is referred as Global variable and can be accessible by other functions. So, option B is correct.

Question 21. What is the output of the following program?

z = lambda x : x * x
print(z(6))

A. 6
B. 36
C. 0
D. error

#Ans : B Explanation: This lambda function will calculate the square of a number. So, option B is correct.

Question 22. What is the output of the following program?

print(chr(ord(chr(97))))
A. a
B. A
C. 97
D. error

#Ans : A Explanation: Outer chr and ord will cancel out and we will be left with chr(97) which will return "a". So, option A is correct.

Question 23. Choose the correct option with reference to below Python code?
```def fn(a):
print(a)
x=90
fn(x)
```

A. x is the formal argument.
B. a is the actual argument.
C. fn(x) is the function signature.
D. x is the actual argument.

#Ans : D Explanation: x is the actual argument that is the correct option with reference to below Python code. So option D is correct.

Question 24. Which one of the following is incorrect?

A. The variables used inside function are called local variables.
B. The local variables of a particular function can be used inside other functions, but these cannot be used in global space
C. The variables used outside function are called global variables
D. In order to change the value of global variable inside function, keyword global is used.

#Ans : B Explanation: The local variables of a particular function can be used inside other functions, but these cannot be used in global space is one of the following is incorrect. So the option B is correct.

## MCQ on Functions in PythonSet 3

Question 1. In Python, how are arguments passed?

A. pass by value
B. pass by reference
C. It gives options to user to choose
D. Both A and B

#Ans : B Explanation: All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function

Question 2. Which of the following is incorrect way of using an argument while defining a function in Python, where var1 and var2 are two arguments?

A. def fn(var1, var2)
B. def fn(var1, var2=5)
C. def fn(*var1, var2)
D. def fn(var1,*var2)

#Ans : C Explanation: def fn(*var1, var2) is incorrect way of using an argument while defining a function in Python.

Question 3. Which of the following is not a type of argument in Python?

A. Keyword
B. Variable argument count
C. Positional
D. None of these

#Ans : D Explanation: All of the above are the type of argument in Python.

Question 4. Which of the following is the property of variable argument count?

A. More than one argument can be used while calling the function.
B. It should be always last while declaring the arguments.
C. Both A and B
D. None of these

#Ans : C Explanation: More than one argument can be used while calling the function and It should be always last while declaring the arguments are the following is the property of variable argument count.

Question 5. Which of the following would result in an error?
```A. def function1(var1=2, var2):
var3=var1+var2
return var3
function1(3)

B. def function1(var1, var2):
var3=var1+var2
return var3
function1(var1=2,var2=3)

C. def function1(var1, var2):
var3=var1+var2
return var3
function1(var2=2,var1=3)

D. def function1(var1, var2=5):
var3=var1+var2
return var3
function1(2,3)
```

#Ans : A Explanation: Option A would result in an error because var1=2 can not be defined before var2.

Question 6. What will be the output of the following Python code?
```def function1(var1=5, var2=7):
var2=9
var1=3
print (var1, " ", var2)
function1(10,12)```

A. 5 7
B. 3 9
C. 10 12
D. Error

#Ans : B Explanation: 3 9 will be the output of the following Python code.

Question 7. What will be the output of the following Python code?
```def function1(var1,var2=5):
var1=2
var3=var1*var2
return var3
var1=3
print(function1(var1,var2)) ```

A. 10
B. 15
C. Error as var2 is not defined while calling the function
D. Does not give any error as var2 is a default argument

#Ans : C Explanation: There will be an error in the code and the error is : Error as var2 is not defined while calling the function.

Question 8. . Which among the following are mutable objects in Python?
(i) List
(ii) Integer
(iii) String
(iv) Tuple

A. i only
B. i and ii only
C. iii and iv only
D. iv only

#Ans : A Explanation: List are mutable objects in Python.

Question 9. What will be the output of the following Python code?
```def function1(var1):
var1=var1+10
print (var1)
var1=12
function1(var1)
print(var1)
A. 22
22
B. 12
12
C. 22
22
D. 12
22

Answer#Ans : C Explanation: 22

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

def fn(var1):
var1.pop(1)
var1=[1,2,3]
fn(var1)
print(var1)

A. [1,2,3]
B. [1,3]
C. [2,3]
D. [1,2]

Answer#Ans : B Explanation: [1,3] will be the output of the following Python code.
```

## we are still adding some more questions.

error: Content is protected !!