This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 5: Advanced function features

Quiz

face Josiah Wang

Try these questions. Note that some of these are trick questions! 😈

For all questions, assume that you have the following function defined.

def calculate(x, y, z=1, negate=False):
    result = (x + y) / z
    if negate:
        result = -result
    return result
1 2 3 4 5 6 7 8 9

Question 1

What is the value of output in the code below? Type "Error" if the result is an error.

output = calculate(6, 4, 2, True)
-5.0
Explanation:

The calculation will be (6+4)/2 which is 5.0. Since the argument for negate is set to True, the function returns -5.0.

Question 2

What is the value of output in the code below? Type "Error" if the result is an error.

output = calculate(6, 2)
8.0
Explanation:

Since z defaults to 1, the calculation will be (6+2)/1 which is 8.0. Since negate defaults to False, the function does not perform any negation and returns 8.0.

Question 3

What is the value of output in the code below? Type "Error" if the result is an error.

output = calculate(6)
Error
Explanation:

Python tells you exactly why: "calculate() missing 1 required positional argument: 'y'".

Question 4

What is the value of output in the code below? Type "Error" if the result is an error.

output = calculate(6, 4, negate=True)
-10.0
Explanation:

Since z defaults to 1, the calculation will be (6+4)/1 which is 10.0. Since the keyword argument for negate is given as True, the function returns its negated value -10.0.

Question 5

What is the value of output in the code below? Type "Error" if the result is an error.

output = calculate(x=4, y=2, z=3, negate=False)
2.0
Explanation:

All keyword arguments are provided by the function caller. So the final result is (4+2)/3 which is 2.0.

Question 6

What is the value of output in the code below? Type "Error" if the result is an error.

output = calculate(z=3, x=4, negate=False, y=2)
2.0
Explanation:

The positioning of keyword arguments does not matter, as long as you provide the mandatory arguments x and y. So the output is just like the previous question: (4+2)/3 which is 2.0.

Question 7

What is the value of output in the code below? Type "Error" if the result is an error.

output = calculate(2, y=4)
6.0
Explanation:

You can supply a keyword argument for a mandatory positional argument, as long as all positional arguments that come before that have been specified.

Question 8

What is the value of output in the code below? Type "Error" if the result is an error.

output = calculate(z=3, 2)
Error
Explanation:

Try this out - what is the error reported by Python? You must always provide the mandatory positional arguments first before supplying any keyword arguments. Do not use keyword arguments and then a positional argument. Otherwise, Python will be completely confused as to how to match the parameters to the argument (should 2 be assigned to x or y?)

Question 9

What is the value of output in the code below? Type "Error" if the result is an error.

output = calculate(x=1, 2, negate=True)
Error
Explanation:

Same as the previous question, you must always provide the mandatory positional arguments first before supplying any keyword arguments. Do not use keyword arguments and then a positional argument, even if the first keyword matches the first positional argument. Otherwise, Python will be completely confused as to how to match the parameters to the argument (should 2 be assigned to x or y?)