Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Functions and Error Handling

Total questions: 20

Worksheet time: 15mins

Name
Class
Date
1.

Which of the following is an advantage of using local variables?

a)

They allow the variable to be used throughout the whole program

b)

They allow variable identifiers to be reused each time

c)

They are easier to program than global variables

d)

A wider range of data types can be used

2.

What term is used to describe data passed into/out of a program?

a)

Variable

b)

Loop

c)

Constant

d)

Parameter

3.

Leo wants to create a function that will roll a dice. Which syntax is correct?

a)

def dice roll ():

b)

def diceroll ()

c)

def diceroll ():

d)

def diceroll []:

4.

What will the output of this program be when it is run?

a)

Nothing

b)

Hello, world

c)

It will generate an error

5.

What will be the output of this program if the user enters "Han" and then "Solo"?

a)

An error

b)

Nothing

c)

first surname

d)

Han Solo

6.
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
7.
How do you correctly call this function
a)
def(1)
b)
x=square( )
c)
defSquare(4)
d)
x=square(5)
8.

What is a parameter in a function? (Select all that apply)

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

9.

What are the reasons for using functions? (Select all that apply, multiple answers)

a)

To build code that will be reused

b)

To simplify code

c)

To add comments

d)

Help us to organize longer programs

10.

What is scope search order?

a)

Local Global Enclosing BuiltIn

b)

Global Local Enclosing Builtin

c)

Local Enclosing Global Builtin

d)

Builtin Local Global Enclosing

11.

What is echo?

def shout_echo(word1, echo = 1):

a)

default parameter, optional argument when call function

b)

default argument, required parameter when call function

c)

default argument, optional parameter when call function

d)

default parameter, required argument when call function

12.

What data type in the following function definition is used to send a flexible number of arguments to the function body?

def sum(*args)

a)

dictionary

b)

tuple

c)

list

d)

string

13.

What data type in the following function definition is used to send a flexible number of arguments to the function body?

def function_name(**kwargs)

a)

dictionary

b)

tuple

c)

list

d)

string

14.

This is an example of a lambda function that would execute correctly and output what number?

raise_to_power = lambda x, y : x ** y

raise_to_power(2,3)

a)

2

b)

3

c)

9

d)

8

15.

nums = [ 1, 3, 4,6,8]

square_all = map(lambda num: num ** 2, nums)

print(square_all)


The above program outputs

a)

error

b)

memory location of square_all

c)

[1, 9, 16, 36, 64]

16.

To catch an exception during program execution, use the try except clause.

a)

True

b)

False

17.

Which of the following would return a ValueError message?

a)

shout_echo("particle", echo=5)

b)

shout_echo("particle")

c)

shout_echo("particle", echo=-1)

d)

shout_echo["particle", echo=5]

18.

In Python the function definition has to be executed before the first time it is called.

a)

False

b)

True

19.

Select all of the following statements that are true for docstrings.

a)

Describe what your function does

b)

Are placed in the immediate line after the function header In between triple double quotes

c)

Serve as the documentation for your function

d)

Are placed in the immediate line after the function header In between triple single quotes

20.

One can return multiple values from a function by using tuples. Select all statements that are NOT true for a tuple.

a)

immutable

b)

can contain multiple values like a list

c)

are constructed using square brackets