WorksheetsPython Functions and Error Handling
Total questions: 20
Worksheet time: 15mins
Which of the following is an advantage of using local variables?
They allow the variable to be used throughout the whole program
They allow variable identifiers to be reused each time
They are easier to program than global variables
A wider range of data types can be used
What term is used to describe data passed into/out of a program?
Variable
Loop
Constant
Parameter
Leo wants to create a function that will roll a dice. Which syntax is correct?
def dice roll ():
def diceroll ()
def diceroll ():
def diceroll []:
What will the output of this program be when it is run?
Nothing
Hello, world
It will generate an error
What will be the output of this program if the user enters "Han" and then "Solo"?
An error
Nothing
first surname
Han Solo
What is a parameter in a function? (Select all that apply)
input value to a function for the function’s execution
a variable used to send information to a function
the name of the function
the name of the main program calling the function
What are the reasons for using functions? (Select all that apply, multiple answers)
To build code that will be reused
To simplify code
To add comments
Help us to organize longer programs
What is scope search order?
Local Global Enclosing BuiltIn
Global Local Enclosing Builtin
Local Enclosing Global Builtin
Builtin Local Global Enclosing
What is echo?
def shout_echo(word1, echo = 1):
default parameter, optional argument when call function
default argument, required parameter when call function
default argument, optional parameter when call function
default parameter, required argument when call function
What data type in the following function definition is used to send a flexible number of arguments to the function body?
def sum(*args)
dictionary
tuple
list
string
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)
dictionary
tuple
list
string
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)
2
3
9
8
nums = [ 1, 3, 4,6,8]
square_all = map(lambda num: num ** 2, nums)
print(square_all)
The above program outputs
error
memory location of square_all
[1, 9, 16, 36, 64]
To catch an exception during program execution, use the try except clause.
True
False
Which of the following would return a ValueError message?
shout_echo("particle", echo=5)
shout_echo("particle")
shout_echo("particle", echo=-1)
shout_echo["particle", echo=5]
In Python the function definition has to be executed before the first time it is called.
False
True
Select all of the following statements that are true for docstrings.
Describe what your function does
Are placed in the immediate line after the function header In between triple double quotes
Serve as the documentation for your function
Are placed in the immediate line after the function header In between triple single quotes
One can return multiple values from a function by using tuples. Select all statements that are NOT true for a tuple.
immutable
can contain multiple values like a list
are constructed using square brackets
