Font size
WorksheetsPython Functions: Userdefined
Total questions: 70
Worksheet time: 57mins
What is a function definition in Python?
A function definition in Python is a single line of code that specifies the function name only.
A function definition in Python is a variable declaration.
A function definition in Python is a block of code that specifies the name of the function, the parameters it takes, and the code block that gets executed when the function is called.
A function definition in Python is a comment that describes what the function does.
Explain the concept of function parameters in Python.
Parameters in Python functions are defined outside the function definition.
Function parameters in Python are optional and not necessary for the function to work.
Function parameters in Python are the output variables that are returned by a function.
Function parameters in Python are the input variables that are passed into a function when it is called. These parameters allow the function to accept data or information to perform its task. Parameters are defined within the parentheses of a function definition.
What does the return statement do in a Python function?
The return statement exits the function and returns a value back to the caller.
The return statement prints a message to the console.
The return statement pauses the function execution.
The return statement loops back to the beginning of the function.
How do you call a function in Python?
function_call()
execute_function()
call_function()
my_function()
Discuss the concept of function scope in Python.
Function scope in Python refers to the visibility of variables within a function. Variables defined inside a function are local to that function and cannot be accessed outside of it unless explicitly returned. Global variables can be accessed within a function, but if a variable with the same name is defined inside the function, it will create a new local variable with the same name.
Function scope in Python refers to the color of variables within a function.
Variables defined inside a function are global to that function.
Global variables cannot be accessed within a function.
Explain the different types of parameters in Python functions.
Named parameters and anonymous parameters
Required parameters and optional parameters
Positional parameters and keyword parameters
Mandatory parameters and default parameters
What is the LEGB rule in Python function scope?
Local, Enclosing, Global, Built-in
Local, Enclosing, Global, Internal
Local, Enclosing, General, Built-in
Local, External, Global, Built-in
How can you define a user-defined function in Python?
function function_name(parameters): # Function body
def function_name(parameters):\n # Function body
define function_name(parameters): # Function body
create function_name(parameters): # Function body
What is the purpose of function parameters?
To provide decoration to the code
To confuse the programmer
To pass values into a function for it to use during its execution.
To increase the function's efficiency
How can you pass arguments to a Python function?
Arguments are passed to a Python function by sending a fax
Arguments are passed to a Python function by placing them inside the parentheses when calling the function.
Arguments are passed to a Python function by using emojis
Arguments are passed to a Python function by telepathy
What happens if a return statement is not included in a Python function?
The function will return 'None'.
The function will return an error message.
The function will return 'True'.
The function will return the input parameter.
What is the difference between actual parameters and formal parameters?
Actual parameters are the values passed to a function when it is called, while formal parameters are the placeholders defined in the function declaration to receive those values.
Actual parameters are placeholders in function declaration, while formal parameters are values passed to a function.
Actual parameters are used in function declaration, while formal parameters are used in function calls.
Actual parameters are defined in the function declaration, while formal parameters are passed to a function when called.
How can you access a variable defined inside a function from outside the function?
Access it directly using the function name
Use a global variable to store the value
Return the variable from the function and then access it by calling the function.
Create a new function to retrieve the variable
What is the default scope of a variable in Python?
local
global
instance
static
Explain the concept of keyword arguments in Python functions.
Keyword arguments are not allowed in Python functions.
Keyword arguments are used to define variables within a function.
Keyword arguments are only supported in Python 2.x versions.
Keyword arguments in Python functions provide a way to pass arguments to a function using parameter names as keys in key-value pairs.
What is the difference between positional arguments and keyword arguments?
Positional arguments are used for functions, while keyword arguments are used for classes.
Positional arguments are required, while keyword arguments are optional.
Positional arguments are passed using a dictionary, while keyword arguments are passed as a list.
Positional arguments are defined by their position, while keyword arguments are identified by the parameter name.
How can you return multiple values from a Python function?
Pass the values as arguments to the function
Return a tuple containing the multiple values
Use global variables to store the values
Return a list containing the multiple values
Discuss the concept of global and local variables in Python functions.
Global variables are limited to the function scope, while local variables are accessible throughout the program.
Global variables are only accessible within the function where they are defined, while local variables are accessible throughout the program.
Global variables and local variables are interchangeable in Python functions.
Global variables are accessible throughout the program, while local variables are limited to the function scope.
What is the default return value for a function that does not return a value explicitly?
None
int
double
null
Which of the following items are present in the function header?
Function name only
Both function name and parameter list
parameter list only
Return value
What Is The Output Of The Following Code Snippet?
def func(message, num = 1):
print(message * num)
func('Welcome')
func('Viewers', 3)
Welcome
Viewers
Welcome
ViewersViewersViewers
Welcome
Viewers,Viewers,Viewers
Welcome
Fill in the line of code for calculating the factorial of a number.
def fact(num):
if num == 0:
return 1
else:
return _____________________
num*fact(num-1)
(num-1)*(num-2)
num*(num-1)
fact(num)*fact(num-1)
What is the output of the following piece of code?
def test(i,j):
if(i==0):
return j
else:
return test(i-1,i+j)
print(test(4,7))
13
7
Infinite Loop
17
Which of the following function calls can be used to invoke the below function definition?
def test(a,b,c,d)
test(1,2,3,4)
test(a=1,2,3,4)
test(a=1,b=2,c=3,4)
test(a=1,b=2,c=3,d=4)
what is a variable defined outside all the function referred to as?
A static variable
A global variable
A local variable
An automatic variable
what is a variable defined inside all the function referred to as?
A static variable
A global variable
A local variable
An automatic variable
Which of the given argument types can be skipped from a function call?
Positional arguments
keyword arguments
named arguments
default arguments
Pick one from the following statements to correctly complete the function body to get the output 5 in the given code snippet:
def f(number):
#missing function body
print(f(5))
return "number"
print(number)
print("number")
return number
A named blocks of code that are designed to do one specific job is called as________
Loop
Function
Block
Branching
While defining a function which of the
following symbol is used.
;(semi colon)
.(dot)
$(dollar)
:(colon)
What is the output of the given code?
10 20 10
10
20
10 20
What is the output of the given code?
10 20 10 20
10 20 10
10 20 20
Error
Function header in Python contains _________________.
function name
list of parameters
return type
all the above
Only A & B
What will be the output of the following Python code?
3
4
4 is maximum
None of the mentioned
What will be the output of the following Python code?
x is 50
Changed global x to 2
Value of x is 50
x is 50
Changed global x to 2
Value of x is 2
x is 50
Changed global x to 50
Value of x is 50
None of the mentioned
What will be the output of the following Python code?
Hello
WorldWorldWorldWorldWorld
Hello
World 5
Hello
World,World,World,World,World
Hello
HelloHelloHelloHelloHello
What will be the output of the following Python code?
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
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
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
None of the mentioned
What will be the output of the following Python code?
2
3
The numbers are equal
None of the mentioned
What will be the output of the following Python code?
9
3
27
30
What will be the output of the following Python code?
212
32
314
24
567
98
None of the mentioned
What will be the output of the following Python code?
212
32
9
27
567
98
None of the mentioned
What will be the output of the following Python code?
Function returns the sum
of all values
6
15
Function returns the sum
of all values
6
100
123
12345
None of the mentioned
What is a variable defined outside a function referred to as?
A static variable
A global variable
A local variable
An automatic variable
What will be the output of the following Python code?
1
Nothing is displayed
0
An exception is thrown
What will be the output of the following Python code?
4
5
1
An exception is thrown
What will be the output of the following Python code?
10
56
45
56
10
20
Syntax Error
What will be the output of the following Python code?
An exception is thrown because of conflicting values
1 2
3 3
3 2
What will be the output of the following Python code?
Integer
Tuple
Dictionary
An exception is thrown
What will be the output of the following Python code?
ZZZ
ZZ
An exception is executed
Infinite loop
1. A function has the following definition:
def function1(x=4,y=3,z=2):
(a) print(x,y,z)
Write the function call such that x, y and z get default values
Predict the output
stop
slow
go
slow
None
go
Predict the output.
Note: end=' ' is a single space.
(a)
Which of the following function headers is correct?
def fun(a = 2, b = 3, c)
def fun(a = 2, b, c = 3)
def fun(a, b = 2, c = 3)
def fun(a, b, c = 3, d)
Which one of the following is incorrect?
The variables used inside function are called local variables
The local variables of a particular function can be used inside other functions, but these cannot be used in global space
The variables used outside function are called global variables
In order to change the value of global variable inside function, keyword global is used.
Predict the output.
Note: end= ' ' is a single space.
20 20
NameError
20 NameError
Can't say
Python function always returns a value
True
False
What is the output of the add() function call
15
8
(8,7)
Syntax Error
Predict the output
value
Second
parameter
First
Consider the code given. Line 1 is called......
Function header
Function body
Function definition
Function name
Predict the output
5
10
32
25
Function defined to achieve some task as per the programmer’s requirement is called a ______________
user defined function
library function
built in functions
All of the above.
Predict the output
(a)
Predict the output. Note: end is a single space
First 10
Second 10
1 10
Error
Predict the output. Note: end is a single space
(a)
Predict the output. Note: end is a space
7 8
7 Error
Error
7 7
Observe the code and choose the incorrect option.
Function name is 's'
n1 is a local variable to the function.
n2 is a global variable and can be accessed from s.
Both the print statements will print different value.
Choose the right answer
0
1
2
3
Choose the correct statement
We can create function with no argument and no return value.
We can create function with no argument and with return value(s)
We can create function with argument(s) and no return value.
All of the above
Predict the output of the following code:
50#50
50#5
50#30
5#50
8. In python if no value is provided in the function call , argument takes ______________ value.
provided
temporary
default
none of these
