WorksheetsCP M.7 Functions Assessment Review
Total questions: 20
Worksheet time: 18mins
Why do we use functions?
To stop the program from repeating.
To test for true and false.
To simplify code.
To ask the user for input.
The _____________ go above the _____________ in the source code file
main program, return
main program, functions
subprograms, main program
def, functions
Does the following code return a string or a number?
def f():
return "3"
string
number
It is necessary to do something to the value returned by a function; otherwise the value will not be stored in memory and will be lost.
True
False
A variable used to send information to a function.
return
Parameter
Tracing code
def
We call the central part of the program the ______________ program
main
def
sub
return
Which of the following parameters is optional?
sample ( a, b, c, d = 121)
a
d
c
b
Consider the following code:
def tryIt (an):
val = an * 2
c = 0
if (val < 0):
c = 1
return c
Which variable is the parameter?
(a)
We (a) to separate the code in functions from the main code.
Consider the following code:
def addIt (a, b):
return a + b
print (a)
What is wrong?
There should be a third parameter.
The method cannot return a value.
The method should only have one parameter.
The print statement cannot be reached.
Consider the following code:
def sub1 (a, b):
return(a + b)
def sub2 (a, b):
return(a * b)
What is output by:
print (sub2(sub1 (2, 3), (sub1(1, 1))))
(a)
Reading through code to find errors and predict results is called (a) .
What is output by the following program?
def mult(a, b = 1, c = 1):
return(a b c)
print(mult(2, 5, 6))
0
60
2
10
Functions can do all of the following except:
Stop the program from repeating.
Allow writing without proper syntax.
Simplify code.
Ask the user for input.
Which keyword is used in functions?
send
stop
go
return
Consider the following program:
def sample(val):
val = val - 9
#MAIN
n = 59
sample(n)
print(n)
What is output?
9
50
59
n
Consider the following code?
def mystery (a, b = 8, c = -6):
return a + b + c
#MAIN
x = int(input("First value: "))
y = int(input("Second value: "))
z = int(input("Third value: "))
print(mystery (x, y, z))
What is output when the user enters 12, 2 and 2?
8
14
16
0
Which of the following parameters is optional?
sample(a, b, c = “Latte”)
a
b
c
none are optional
What is output by the following program?
def test():
print("Rainbow")
#MAIN
test()
print("Sunshine")
print("Storm.")
Sunshine Storm. Rainbow
Rainbow Sunshine Storm.
Storm. Sunshine Rainbow
print("Rainbow") Sunshine Storm
What is output by the following program?
def mult(a, b = 1, c = 1):
return(a b c)
print(mult(2, 5, 8))
0
80
2
10
