Lesson 4.12.2.1
4.12.2.1 Writing functional programs Quiz: AQA Computer Science, Unit 12
20 questions
In partnership with Revision Ninja
Lesson 4.12.2.1, Writing functional programs: 20 multiple choice questions for the AQA Computer Science (7517), Unit 12: Fundamentals of functional programming, written with Revision Ninja.
Host it live on the board and students join with a game code on their own devices, or revise alone with Free Play. The answers are revealed in the game.
The 20 questions
-
What is a higher-order function?
- A function that can only be called from another module at a higher level.
- A function that takes a function as an argument, returns a function as a result, or does both.
- A function that runs at a higher priority than the other functions in the program.
- A function that is written in a higher-level language than the rest of the program, so that it can call the others without any extra setup.
-
What does map do?
- Returns the first element of a list together with the remaining elements of the list.
- Combines the elements of a list into a single value using a combining function.
- Applies a given function to each element of a list, returning a list of the results.
- Returns only the elements of a list that satisfy a given condition in the list.
-
What does filter do?
- Combines all the elements of a list into one value by repeatedly applying a function.
- Applies a function to every element of a list and returns the results in order.
- Returns a new list containing exactly those elements that satisfy a given condition.
- Returns the number of elements in a list without changing any of the elements.
-
What does reduce or fold do?
- Creates a new list containing only the first few elements of the original list.
- Sorts a list into ascending order using a supplied comparison function for values, which returns the list in a new and sorted order.
- Applies a condition to each element and keeps only those that fail the condition.
- Combines the values of a list into a single value by repeatedly applying a combining function.
-
What is the result of applying a function that doubles its input to every element of [1,2,3] using map?
- [3,4,5]
- [6]
- [1,2,3]
- [2,4,6]
-
What is the result of filter (> 1) applied to [1,2,3]?
- [3]
- [2,3]
- [1]
- [1,2,3]
-
What is the result of fold (+) 0 applied to [1,2,3,4]?
- 24
- 10
- 4
- 0
-
Which of these is a higher-order function?
- length, because it returns the number of items that are in a list.
- head, because it returns only the first element of the list it is given.
- map, because it takes a function and a list as its arguments.
- +, because it simply adds two numbers together and returns the total.
-
Which function call returns only the even numbers from [1,2,3,4]?
- map even [1,2,3,4]
- fold even [1,2,3,4]
- filter even [1,2,3,4]
- head [1,2,3,4]
-
Using fold, how would you find the largest number in a non-empty list?
- Fold with a function that returns the larger of its two arguments.
- Head applied to the list after it has been sorted into reverse order.
- Map with a function that returns the smaller of two numbers from the list.
- Filter with a function that removes every number from the list in turn.
-
A program needs the square of every number in a list. Which higher-order function is most suitable?
- fold, with a function that adds each square to a running total for the list.
- map, with a function that squares each element.
- reverse, with a function that squares each element before the list is reversed.
- filter, with a function that squares each element and removes the original value.
-
Which statement correctly describes a function that returns a function?
- Returning a function is only possible when the function has no arguments in its definition.
- A function that returns a function must always return the same function on every call.
- A higher-order function may return a new function, such as one created by partially applying add to 4.
- A function cannot return another function in any functional programming language at all.
-
To count how many elements of a list are negative, which combination is most natural?
- fold with a multiplication function, then tail of the list that is supplied.
- reduce with a sorting function, then length of the original list before filtering.
- filter with a test for negative numbers, then length of the result.
- map with a test for negative numbers, then head of the result that is produced.
-
Which statement about the length of map's output is correct?
- map returns a list of the same length as its input list.
- map returns a list with one element for each pair of elements in the input list.
- map returns a list that is always shorter than the list it was given as input.
- map returns a single value, so the length of its result is always one element.
-
Which statement about filter's output is correct?
- The result contains only the elements that satisfy the condition, kept in their original order.
- The result contains only the elements that fail the condition, sorted into ascending order by value, so the largest values appear last.
- The result contains every element of the list, each one changed by the condition in turn.
- The result is always a single value, which is the number of elements that pass the test.
-
Evaluate: why are map, filter and fold preferred in many functional programs over loops that change a variable?
- They let the programmer change a global variable from many places in the program at once.
- They run faster on every machine because they bypass the processor and use memory directly.
- They are the only way to write any kind of loop in a functional language at all.
- They avoid changing shared state, which makes the code easier to reason about and to run in parallel.
-
In a language with first-class functions, why can map be written as a general-purpose tool?
- Because map always applies the same built-in addition to every element of the list.
- Because map changes the original list directly in memory without making a copy of it.
- Because map can only be used with lists of integers as its input and nothing else.
- Because the function to apply is passed as an argument, so map does not need to know what that function does.
-
A list of records must be grouped and totalled. Which higher-order function is most suitable for building the total?
- map, applied to the list to remove each record before the total is built.
- length, applied to the list to total the records and then group them together, which returns the record count for each group in the list.
- fold, applying a combining function to each record in turn to build the total.
- filter, applied to the list to keep only the record names for the total.
-
Which statement correctly contrasts map and fold?
- map returns the first element of a list, whereas fold returns the last element of the list.
- map keeps one output for each input element, whereas fold combines all of the elements into one value.
- map and fold always produce identical results when they are applied to the same list.
- fold keeps one output for each input element, whereas map combines all of the elements of the list into one single value at the end.
-
Which is true of higher-order functions in a language such as Haskell?
- They let a program be built from small general functions, each of which is passed the specific behaviour it needs.
- They are only available in programs that also rely on loops and changeable variables.
- They are not allowed to call other functions, so they must be written as one large block.
- They can only work with numbers and cannot be used to process strings or lists at all.
Related quizzes
- Function types and first-class objects Quiz · 4.12.1.1 · 20 questions
- Partial function application and composition of functions Quiz · 4.12.1.4 · 20 questions
- List processing Quiz · 4.12.3.1 · 20 questions
- Data types Quiz · 4.1.1.1 · 20 questions
- Entity relationship modelling Quiz · 4.10.1.1 · 20 questions
- Big Data Quiz · 4.11.1.1 · 20 questions
- Analysis Quiz · 4.13.1.1 · 20 questions
- Data structures and abstract data types Quiz · 4.2.1.1 · 20 questions
- Breadth-first and depth-first search Quiz · 4.3.1.1 · 20 questions
- Problem-solving and algorithms Quiz · 4.4.1.1 · 20 questions