Back to the topic

The mark scheme starts on a new page, so you can print pages one and two for a student and keep the last page yourself. Everything prints in black on white whichever theme you are using.

Python · A Level 2

Functional Programming

Section A: 10 multiple choice questions, 10 marks. Section B: 5 written questions, 17 marks. Total 27 marks.

Name

Class

Date

Mark  / 27

Section A

Circle the letter of the correct answer. One mark each.

  1. 1What does it mean for a function to be a first-class object?1
    1. AIt can be stored in a variable, passed to a function and returned from a function
    2. BIt is the first function that the program calls when it starts to run
    3. CIt is a function that can only be called once and is then deleted
    4. DIt is a function that takes no parameters and returns no value
  2. 2What does map(f, items) return?1
    1. AThe first item with f applied
    2. BA single value combining every item
    3. COnly the items for which f is true
    4. DThe results of applying f to every item
  3. 3What does filter(f, items) return?1
    1. AA single combined value
    2. BThe items for which f returns False
    3. CThe items for which f returns True
    4. DThe results of applying f to every item
  4. 4What does reduce(lambda a, b: a + b, [1, 2, 3, 4]) return?1
    1. A10
    2. B24
    3. C[3, 6, 10]
    4. D[1, 2, 3, 4]
  5. 5Which describes a pure function?1
    1. AIt never takes any arguments
    2. BIt gives the same output for the same input and has no side effects
    3. CIt changes a global variable only once
    4. DIt uses only local variables but may print to the screen
  6. 6Which of these is a side effect?1
    1. ACreating a local variable inside the body of the function
    2. BBuilding and returning a brand new list from the arguments
    3. CChanging the contents of a list that was passed in as an argument
    4. DCalling a different function that is itself pure
  7. 7Why is immutable data useful in functional programming?1
    1. AIt always uses less memory than mutable data
    2. BIt allows a function to have side effects
    3. CIt makes recursion unnecessary
    4. DA value cannot be changed unexpectedly by another part of the program
  8. 8What are head([4, 5, 6]) and tail([4, 5, 6])?1
    1. A6 and [4, 5]
    2. B4 and [5, 6]
    3. C4 and 6
    4. D[4] and [5, 6]
  9. 9What is partial application?1
    1. ASupplying some of a function's arguments to get a new function that takes the rest
    2. BCalling a function with no arguments so that it uses its default values
    3. CApplying a function to the first half of a list and ignoring the rest
    4. DRunning only the first part of a function and then stopping early
  10. 10What is composition of two functions?1
    1. AWriting one inside the other so neither can be called
    2. BApplying both to the same input and adding the results
    3. CDefining both in the same file
    4. DCombining them so that the output of one is the input of the other

Section B

Answer in the space provided. The number of marks tells you how many separate points to make.

  1. 1StateState what is meant by a first-class object.2
  2. 2DescribeDescribe what the higher-order function filter does.3
  3. 3ShowThe program shown is run. Show the result of each stage of the pipeline and state the value that is output.4
    Python
    from functools import reduce
    
    nums = [3, 8, 1, 6]
    result = reduce(lambda a, b: a + b,
                    map(lambda n: n * 2,
                        filter(lambda n: n > 2, nums)))
    print(result)
  4. 4ExplainExplain why a pure function is easier to test than a function that has side effects.4
  5. 5WriteWrite a Python function called make_multiplier that takes a number n and returns a new function. The new function takes a value x and returns x multiplied by n. Show how it could be used to create a function that triples a number.4

Mark scheme

Section A

1A2D3C4A5B6C7D8B9A10D

Section B

1 State what is meant by a first-class object. 2

One mark for each of the following, to a maximum of 2.

  • A value that can be assigned to a variable
  • It can be passed as an argument to, or returned as the result of, a function

Model answer. A first-class object is a value that can be treated like any other data. It can be assigned to a variable or stored in a data structure, passed as an argument to a function and returned as the result of a function. In functional programming, functions are first-class objects.

2 Describe what the higher-order function filter does. 3

One mark for each of the following, to a maximum of 3.

  • It takes a function and a list as arguments
  • The function is applied to each item and returns true or false
  • It returns a new list containing only the items for which the function returned true

Model answer. Filter takes a function that returns true or false, and a list. It applies the function to each item in the list, and returns a new list that contains only those items for which the function returned true. The original list is not changed.

3 The program shown is run. Show the result of each stage of the pipeline and state the value that is output. 4

One mark for each of the following, to a maximum of 4.

  • filter keeps 3, 8 and 6
  • map doubles them to 6, 16 and 12
  • reduce adds them together
  • The output is 34

Model answer. filter keeps the numbers greater than 2, which gives 3, 8 and 6. map doubles each of them, giving 6, 16 and 12. reduce then adds the values together, 6 + 16 = 22 and 22 + 12 = 34, so the value output is 34.

4 Explain why a pure function is easier to test than a function that has side effects. 4

One mark for each of the following, to a maximum of 4.

  • A pure function always returns the same output for the same input
  • So a test only needs to supply the arguments and check the returned value
  • A function with side effects may change or depend on something outside it, such as a global variable or file
  • So the test would have to set up and check that outside state as well, and results may differ between runs

Model answer. A pure function always returns the same output for the same arguments and does nothing else, so a test only has to supply the arguments and compare the returned value with what is expected. A function with side effects may depend on or change something outside itself, such as a global variable or a file, so a test would also have to set up that state beforehand and check it afterwards, and the same call might give different results on different runs.

5 Write a Python function called make_multiplier that takes a number n and returns a new function. The new function takes a value x and returns x multiplied by n. Show how it could be used to create a function that triples a number. 4

One mark for each of the following, to a maximum of 4.

  • Defines make_multiplier with parameter n
  • Defines an inner function, or a lambda, that takes x
  • The inner function returns x * n, using n from the enclosing function
  • The outer function returns the inner function, and triple = make_multiplier(3) is used

Model answer.

Python
def make_multiplier(n):
    def multiply(x):
        return x * n
    return multiply

triple = make_multiplier(3)
print(triple(7))

make_multiplier returns the inner function multiply, which still has access to n after make_multiplier has finished. That is a closure. triple is a function that multiplies by 3, so triple(7) outputs 21.