01 Working With Function
01 Working With Function
MCQs
1. What is the default return value for a function that does not return a
value explicitly?
(a) None (b) int
(c) double (d) null
4. What is the name given to that are of memory where the system stores
the parameters and local variable of a function call?
(a) a heap (b) storage area
(c) a stack (d) an array
10. Which of the following function calls can be used to invoke the below
function definition?
def test(a, b, c, d)
(a) test(1, 2, 3, 4) (b) test(a = 1, 2, 3, 4)
(c) test(a = 1, b = 2, c = 3, 4) (d) test(a = 1, b = 2, c = 3, d = 4)
11. Which of the following function calls will cause Error while invoking
the below function definition?
def test(a, b, c, d)
(a) test(1, 2, 3, 4) (b) test(a = 1, 2, 3, 4)
(c) test(a = 1, b = 2, c = 3, 4) (d) test(a = 1, b = 2, c = 3, d = 4)
14. What is a variable defined outside all the functions referred to as?
(a) A static variable (b) A global variable
(c) A local variable (d) An automatic variable
15. Carefully observe the code and give the answer.
def function1(a):
a= a + '1'
a=a*2
>>> function1("hello")
(a) indentation Error
(b) cannot perform mathematical operation on strings
(c) hello2
(d) hello2hello2
18. Which of the given argument types can be skipped from a function
call?
(a) positional arguments (b) keyword arguments
(c) named arguments (d) default arguments
Extras
19. Functions that do not return any value are known as:
(a) fruitful functions (b) void functions
(c) library functions (d) user-defined functions
21. Which of the following arguments works with implicit values that are
used if no value is provided?
(a) keyword (b) required
(c) variable-length (d) default
Type A Questions
1. What is a function?
Ans. A function is a set of instructions or subprograms that are used to fulfil
the user’s need. In other words, a function is a bunch of code which
performs a specific task.
2. Why do programmers need functions in python programming?
Ans.
(a) To make the program easy
(b) Divide the large program into a small block of codes
(c) Reduce the lines of code
(d) Easy to update
3. Write the types of functions supported by Python.
Ans. There are three types of functions supported by python.
(a) Built-in functions.
(b) Function defined in modules.
(c) Functions defined by user(User defined functions).
4. What is the local variable and global variable? Explain with an
example.
Ans.
Global Variable: A variable that is declared in top-level statements is called
a global variable. To access the value of a global variable user need to write
a global keyword in front of the variable in a function.
Local Variable: A name declared in a specific function body is called a local
variable
5. What are the arguments supported by python? Explain each of them
with a suitable example.
Ans. Python supports four argument types:
(a) Positional Arguments: Arguments passed to a function in correct
positional order, no. of arguments must match with no. of parameters
required.
(b) Default Arguments: Assign a default value to a certain parameter, it is
used when the user knows the value of the parameter, default values are
specified in the function header. It is optional in the function call statement.
If not provided in the function call statement then the default value is
considered. Default arguments must be provided from right to left.
(c) Key Word Arguments: Keyword arguments are the named arguments
with assigned values being passed in the function call statement, the user
can combine any type of argument.
(d) Variable Length Arguments: It allows the user to pass as many
arguments as required in the program. Variable-length arguments are
defined with the * symbol.
These are specified during the Values passed during the function call.
function definition.
They are also known as formal They are also known as actual
parameters. parameters.
Parameters Arguments
These variables help in the complete These variables are passed to the
execution of the function. function for execution.
Type B Questions
1. What are the errors in following codes? Correct the code and predict
output:
(a)
total = 0 ;
def sum( arg1, arg2 ):
total = arg1 + arg2 ;
print("Total :", total)
return total ;
sum( 10, 20 ) ;
print("Total :", total)
Ans. This code contains following errors.
1. There should not be indentation in line 2 ‘def sum(arg1, arg2):’.
2. There should be one indent in line 5 ‘return total’
3. There should be no semi-column at the end of line 1, 5 and 6.
The correct code is as follows:
total = 0
def sum(arg1, arg2):
total = arg1 + arg2
print("Total :",total)
return total
sum(10,20)
print("Total :",total)
output:
Total : 30
Total : 0
(b)
def Tot (Number) #Method to find Total
Sum = 0
for C in Range (1, Number + 1):
Sum += C
RETURN Sum
print (Tot[3]) #Function Calls
print (Tot[6])
Q5. Write a function Vowels (LIST) in Python, which accepts a list LIST
of strings. Return a list containing only those strings which starts with a
vowel. Sample Input Data of the list
LIST= [“it”, ”is”, ”over”, ”Anakin”, ”I”, ”have”, ”high”, ”ground”]
NEWLIST = [“it”, “is”, “over”, “Anakin”, ”I”]
6. Write a generator function generatesq() that displays the squareroots of
numbers from 100 to n where n is passed as an argument.