Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Functions and Methods

Total questions: 71

Worksheet time: 53mins

Name
Class
Date
1.

which keyword is used to import the modules in the python ?

a)

import

b)

importing

c)

imported

d)

none

2.

if we only import the module then we need to use the function like -

a)

function

b)

module.function()

c)

both

d)

none

3.

which statement is true about a module ?

a)

module is the python program which describes functions, classes or variables.

b)

module is a function in python

c)

module can be anything in python

d)

none

4.

In how many ways we can import a module ?

a)

1

b)

2

c)

3

d)

none

5.

python modules are saved with the extension -

a)

.mod

b)

.module

c)

.py

d)

none

6.

What will be the output of the following code ?

a)

25

b)

0

c)

5

d)

none

7.

which keyword is used to import the modules in the python ?

a)

import

b)

importing

c)

imported

d)

none

8.

What is a library in python?

a)

COLLECTION OF FILES

b)

COLLECTION OF MODULES

c)

IT IS SUBPROGRAM

d)

NAME OF A FILE

9.

Which of the statements is used to import all names from a module into the current calling module

a)

import

b)

from

c)

dir()

d)

import*

10.

exp(),floor()belongs which module?

a)

cmath.py

b)

urlib.py

c)

math.py

d)

statistics.py

11.

What will be the output of the following

from import factorial

print(math.factorial(5))

a)

25

b)

10

c)

120

d)

error bcz factorial is not a module in math

12.

This program will produce

a)

a random number between 10 and 100 (including 10 but not 100) always in increments of 10

b)

a random number between 10 and 100 (including 10 and 100) always in increments of 10

c)

10 random numbers between 10 and 100 (including 10 and 100)

d)

10 random numbers between 10 and 100 (including 10 and not 100)

13.

2. Which of the following functions is a built-in function in

python?

a)

a) seed()

b)

b) sqrt()

c)

c) factorial()

d)

d) print()

14.

Suppose a function called add() is defined in a module called adder.py. Which of the following code snippets correctly show how to import and use the add() function? Select all that apply.

a)

import adder


result = adder.add(2, 3)

b)

from adder import add function


result = add(2, 3)

c)

from adder file import add


result = adder.add(2, 3)

d)

import add from adder


result = add(2, 3)

15.

Which of the following is not an advantage of using modules?

a)

Provides a means of testing individual parts of the program

b)

Provides a means of reducing the size of the program

c)

Provides a means of dividing up tasks

d)

Provides a means of reuse of program code

16.

What will be the output of the following Python function if the random module has already been imported?


random.randint(3.5,7)

a)

Error

b)

The integer closest to the mean of 3.5 and 7

c)

Any integer between 3.5 and 7, excluding 7

d)

Any integer between 3.5 and 7, including 7

17.

Which keyword marks the beginning of the function block?

(a)  

18.

What is the another term for calling a function?

(a)  

19.

Write the name of the function which is not returning any value.

(a)  

20.

The values being passed through a function-call statement are called,

(a)  

21.

The values received in the function definition header are called ,

(a)  

22.

Python refers to the values being passed are called,

(a)  

23.

In function where the values being received are known as,

(a)  

24.

What is the name of the function in the picture given?

(a)  

25.

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

26.

Which of the following keywords marks the begining of the function block?

a)

Func

b)

define

c)

def

d)

function

27.

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

28.

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)

29.

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

30.

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)

31.

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

32.

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

33.

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

34.

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

35.

What is a piece of code that can take inputs to perform a certain task?

a)

a function

b)

arguments

c)

parameters

36.

Consider the line of code:

Label('Go Team', 200, 100)

What is the Label?

a)

argument

b)

parameter

c)

function name

37.

Consider the line of code:

Label('Go Team', 200, 100)

What is 'Go Team' ?

a)

argument

b)

parameter

c)

function name

38.

When defining a function, the definition must occur before it is called.

a)

true

b)

false

39.

Which of the following terms best describes the code above?

a)

function call

b)

function definition

40.

What is the name for the code highlighted in red?

a)

function name

b)

function definition

c)

parameters

d)

arguments

41.

Line 6 is called

a)

function definition

b)

function call

c)

function name

42.

The code highlighted in blue is best defined as:

a)

function name

b)

function call

c)

function definition

d)

arguments

e)

parameters

43.

What is a parameter in a function?

a)

input value to a function for the function’s execution

b)

a variable used to send information to a function

c)

the name of the function

d)

the name of the main program calling the function

44.

The output of the code above will be

a)

0

b)

1

c)

99

45.

Which of the following code is true

a)

square() will return 25

b)

square(3) will return 9

46.

square איך מזמנים את הפונקציה

a)

def(1)

b)

x=square( )

c)

defSquare(4)

d)

x=square(5)

47.

How many values a function may return?

(a)  

48.

What types of values remain unaffected during function call statement?

a)

Mutable objects

b)

Immutable objects

c)

Literal

d)

Variables

49.

A variable defined outside all functions have the lifetime of

(a)  

50.

Write the line of code that will call the function.

(a)  

51.

Write the line of code that will define the function, draw_coin

(a)  

52.

Do you call or define a function first?

a)

call

b)

define

53.

Rewrite this line of code correctly!

(a)  

54.

Rewrite this line of code correctly!

(a)  

55.

Rewrite this line of code correctly!


(Hint: combine the words 'draw' and 'shape' into one word)

(a)  

56.

Rewrite the line of code that is written incorrectly to make it correct!


Hint: The mistake is when the function gets called

(a)  

57.

Write the line of code that will define the function, draw_hat

(a)  

58.

Which is the proper way to call a function?

a)
b)
c)
d)
59.

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

a)

a) function name

b)

b) Parameter list

c)

c) return value

d)

d) Both A and B

60.

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

a)

a) None

b)

b) 0

c)

c) Null

d)

d) Arbitary value

61.

11. What will be the output of the following Python function?

import math

abs(math.sqrt(25))

a)

a) Error

b)

b) -5

c)

c) 5

d)

d) 5.0

62.

13. Which of the following operators is the correct option for power(ab)?

a)

a) a ^ b

b)

b) a**b

c)

c) a^^ b

d)

d) a ^ * b

63.

What is the output of the following code snippet?

a)

1 3

b)

2 3

c)

The program has a runtime error because x and y are not defined.

d)

3 3

e)

3 2

64.

What is the output of the following code snippet?

a)

3 1

b)

3

1

c)

The program has a runtime error because x is not defined.

d)

3

3

e)

1 1

65.

What is the output of the following code snippet?

a)

4 1

b)

4

1

c)

The program has a runtime error because the local variable ‘num’ referenced before assignment.

d)

4

4

e)

4 4

66.
Which of the following function headers is correct?
a)
def f(a = 1, b = 1, c = 2, d):
b)
def f(a = 1, b = 1, c = 2):
c)
def f(a = 1, b, c = 2):
d)
def f(a = 1, b):
67.

What is the output of the following code snippet?

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

68.
How many times will Boo print?
a)
none
b)
4
c)
5
d)
1
69.
Which of the following best describes the order in which these lines are processed in Python?
a)
1,2,3,4
b)
1,2,1,2,1,4
c)
1,2,1,2,1,2,4
d)
1,2,1,2,1,2,1,2,4
70.

If we want to create a Python function that will roll dice for us, which of the below answers will run correctly?

a)

def dice roll ():

b)

def diceroll ()

c)

def diceroll ():

d)

def diceroll []:

71.
Which of the following best describes the order in which these lines are processed in Python?
a)
4, 1, 2
b)
4, 2, 1
c)
1, 2, 3, 4
d)
3, 1, 2