wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Functions: Userdefined

Total questions: 70

Worksheet time: 57mins

Name
Class
Date
1.

What is a function definition in Python?

a)

A function definition in Python is a single line of code that specifies the function name only.

b)

A function definition in Python is a variable declaration.

c)

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.

d)

A function definition in Python is a comment that describes what the function does.

2.

Explain the concept of function parameters in Python.

a)

Parameters in Python functions are defined outside the function definition.

b)

Function parameters in Python are optional and not necessary for the function to work.

c)

Function parameters in Python are the output variables that are returned by a function.

d)

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.

3.

What does the return statement do in a Python function?

a)

The return statement exits the function and returns a value back to the caller.

b)

The return statement prints a message to the console.

c)

The return statement pauses the function execution.

d)

The return statement loops back to the beginning of the function.

4.

How do you call a function in Python?

a)

function_call()

b)

execute_function()

c)

call_function()

d)

my_function()

5.

Discuss the concept of function scope in Python.

a)

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.

b)

Function scope in Python refers to the color of variables within a function.

c)

Variables defined inside a function are global to that function.

d)

Global variables cannot be accessed within a function.

6.

Explain the different types of parameters in Python functions.

a)

Named parameters and anonymous parameters

b)

Required parameters and optional parameters

c)

Positional parameters and keyword parameters

d)

Mandatory parameters and default parameters

7.

What is the LEGB rule in Python function scope?

a)

Local, Enclosing, Global, Built-in

b)

Local, Enclosing, Global, Internal

c)

Local, Enclosing, General, Built-in

d)

Local, External, Global, Built-in

8.

How can you define a user-defined function in Python?

a)

function function_name(parameters): # Function body

b)

def function_name(parameters):\n # Function body

c)

define function_name(parameters): # Function body

d)

create function_name(parameters): # Function body

9.

What is the purpose of function parameters?

a)

To provide decoration to the code

b)

To confuse the programmer

c)

To pass values into a function for it to use during its execution.

d)

To increase the function's efficiency

10.

How can you pass arguments to a Python function?

a)

Arguments are passed to a Python function by sending a fax

b)

Arguments are passed to a Python function by placing them inside the parentheses when calling the function.

c)

Arguments are passed to a Python function by using emojis

d)

Arguments are passed to a Python function by telepathy

11.

What happens if a return statement is not included in a Python function?

a)

The function will return 'None'.

b)

The function will return an error message.

c)

The function will return 'True'.

d)

The function will return the input parameter.

12.

What is the difference between actual parameters and formal parameters?

a)

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.

b)

Actual parameters are placeholders in function declaration, while formal parameters are values passed to a function.

c)

Actual parameters are used in function declaration, while formal parameters are used in function calls.

d)

Actual parameters are defined in the function declaration, while formal parameters are passed to a function when called.

13.

How can you access a variable defined inside a function from outside the function?

a)

Access it directly using the function name

b)

Use a global variable to store the value

c)

Return the variable from the function and then access it by calling the function.

d)

Create a new function to retrieve the variable

14.

What is the default scope of a variable in Python?

a)

local

b)

global

c)

instance

d)

static

15.

Explain the concept of keyword arguments in Python functions.

a)

Keyword arguments are not allowed in Python functions.

b)

Keyword arguments are used to define variables within a function.

c)

Keyword arguments are only supported in Python 2.x versions.

d)

Keyword arguments in Python functions provide a way to pass arguments to a function using parameter names as keys in key-value pairs.

16.

What is the difference between positional arguments and keyword arguments?

a)

Positional arguments are used for functions, while keyword arguments are used for classes.

b)

Positional arguments are required, while keyword arguments are optional.

c)

Positional arguments are passed using a dictionary, while keyword arguments are passed as a list.

d)

Positional arguments are defined by their position, while keyword arguments are identified by the parameter name.

17.

How can you return multiple values from a Python function?

a)

Pass the values as arguments to the function

b)

Return a tuple containing the multiple values

c)

Use global variables to store the values

d)

Return a list containing the multiple values

18.

Discuss the concept of global and local variables in Python functions.

a)

Global variables are limited to the function scope, while local variables are accessible throughout the program.

b)

Global variables are only accessible within the function where they are defined, while local variables are accessible throughout the program.

c)

Global variables and local variables are interchangeable in Python functions.

d)

Global variables are accessible throughout the program, while local variables are limited to the function scope.

19.

What is the default return value for a function that does not return a value explicitly?

a)

None

b)

int

c)

double

d)

null

20.

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

a)

Function name only

b)

Both function name and parameter list

c)

parameter list only

d)

Return value

21.

What Is The Output Of The Following Code Snippet?


def func(message, num = 1):

print(message * num)


func('Welcome')

func('Viewers', 3)

a)

Welcome

Viewers

b)

Welcome

ViewersViewersViewers

c)

Welcome

Viewers,Viewers,Viewers

d)

Welcome

22.

Fill in the line of code for calculating the factorial of a number.


def fact(num):

if num == 0:

return 1

else:

return _____________________

a)

num*fact(num-1)

b)

(num-1)*(num-2)

c)

num*(num-1)

d)

fact(num)*fact(num-1)

23.

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

a)

13

b)

7

c)

Infinite Loop

d)

17

24.

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)

25.

what is a variable defined outside all the function referred to as?

a)

A static variable

b)

A global variable

c)

A local variable

d)

An automatic variable

26.

what is a variable defined inside all the function referred to as?

a)

A static variable

b)

A global variable

c)

A local variable

d)

An automatic variable

27.

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

28.

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

a)

return "number"

b)

print(number)

c)

print("number")

d)

return number

29.

A named blocks of code that are designed to do one specific job is called as________

a)

Loop

b)

Function

c)

Block

d)

Branching

30.

While defining a function which of the

following symbol is used.

a)

;(semi colon)

b)

.(dot)

c)

$(dollar)

d)

:(colon)

31.

What is the output of the given code?

a)

10 20 10

b)

10

c)

20

d)

10 20

32.

What is the output of the given code?

a)

10 20 10 20

b)

10 20 10

c)

10 20 20

d)

Error

33.

Function header in Python contains _________________.

a)

function name

b)

list of parameters

c)

return type

d)

all the above

e)

Only A & B

34.

What will be the output of the following Python code?

a)

3

b)

4

c)

4 is maximum

d)

None of the mentioned

35.

What will be the output of the following Python code?

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

36.

What will be the output of the following Python code?

a)

Hello

WorldWorldWorldWorldWorld

b)

Hello

World 5

c)

Hello

World,World,World,World,World

d)

Hello

HelloHelloHelloHelloHello

37.

What will be the output of the following Python code?

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

38.

What will be the output of the following Python code?

a)

2

b)

3

c)

The numbers are equal

d)

None of the mentioned

39.

What will be the output of the following Python code?

a)

9

b)

3

c)

27

d)

30

40.

What will be the output of the following Python code?

a)

212

32

b)

314

24

c)

567

98

d)

None of the mentioned

41.

What will be the output of the following Python code?

a)

212

32

b)

9

27

c)

567

98

d)

None of the mentioned

42.

What will be the output of the following Python code?

a)

Function returns the sum

of all values

6

15

b)

Function returns the sum

of all values

6

100

c)

123

12345

d)

None of the mentioned

43.

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

44.

What will be the output of the following Python code?

a)

1

b)

Nothing is displayed

c)

0

d)

An exception is thrown

45.

What will be the output of the following Python code?

a)

4

b)

5

c)

1

d)

An exception is thrown

46.

What will be the output of the following Python code?

a)

10

56

b)

45

56

c)

10

20

d)

Syntax Error

47.

What will be the output of the following Python code?

a)

An exception is thrown because of conflicting values

b)

1 2

c)

3 3

d)

3 2

48.

What will be the output of the following Python code?

a)

Integer

b)

Tuple

c)

Dictionary

d)

An exception is thrown

49.

What will be the output of the following Python code?

a)

ZZZ

b)

ZZ

c)

An exception is executed

d)

Infinite loop

50.

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

51.

      Predict the output

a)

stop

slow

go

b)

slow

c)

None

d)

go

52.

Predict the output.

Note: end=' ' is a single space.

(a)  

53.

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)

54.

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.

55.

Predict the output.

Note: end= ' ' is a single space.

a)

20 20

b)

NameError

c)

20 NameError

d)

Can't say

56.

Python function always returns a value

a)

True

b)

False

57.

What is the output of the add() function call

a)

15

b)

8

c)

(8,7)

d)

Syntax Error

58.

Predict the output

a)

value

b)

Second

c)

parameter

d)

First

59.

Consider the code given. Line 1 is called......

a)

Function header

b)

Function body

c)

Function definition

d)

Function name

60.

Predict the output

a)

5

b)

10

c)

32

d)

25

61.

Function defined to achieve some task as per the programmer’s requirement is called a ______________

a)

user defined function

b)

library function

c)

built in functions

d)

All of the above.

62.

Predict the output

(a)  

63.

Predict the output. Note: end is a single space

a)

First 10

b)

Second 10

c)

1 10

d)

Error

64.

Predict the output. Note: end is a single space

(a)  

65.

Predict the output. Note: end is a space

a)

7 8

b)

7 Error

c)

Error

d)

7 7

66.

Observe the code and choose the incorrect option.

a)

Function name is 's'

b)

n1 is a local variable to the function.

c)

n2 is a global variable and can be accessed from s.

d)

Both the print statements will print different value.

67.

Choose the right answer

a)

0

b)

1

c)

2

d)

3

68.

Choose the correct statement

a)

We can create function with no argument and no return value.

b)

We can create function with no argument and with return value(s)

c)

We can create function with argument(s) and no return value.

d)

All of the above

69.

Predict the output of the following code:

a)

50#50

b)

50#5

c)

50#30

d)

5#50

70.

8. In python if no value is provided in the function call , argument takes ______________ value.

a)

provided

b)

temporary

c)

default

d)

none of these