Higher-Order Functions u0026 Function Composition Essentials Quiz

Explore key concepts of higher-order functions and function composition, covering basic definitions, usage, and common patterns to help you grasp core programming paradigms in functional programming and modern coding. Ideal for those seeking foundational understanding of how functions interact, transform, and combine in code.

  1. Definition of Higher-Order Function

    Which of the following best defines a higher-order function in programming?

    1. A function that takes other functions as arguments or returns a function as a result.
    2. A function with more than two parameters.
    3. A function that is always recursive.
    4. A function that only works with numbers.

    Explanation: A higher-order function is defined by its ability to accept functions as parameters and/or return functions. Functions that only handle numbers are not necessarily higher-order. Parameter count alone does not define a higher-order function. Recursion is unrelated to whether a function is higher-order.

  2. Recognizing a Higher-Order Function Example

    Given the function applyTwice(f, x) that returns f(f(x)), what makes applyTwice a higher-order function?

    1. It contains a return statement.
    2. It takes a function f as an argument.
    3. It iterates over a list.
    4. It uses two variables.

    Explanation: applyTwice is higher-order because it accepts a function as an input. The use of two variables does not qualify it as higher-order. Including a return statement is standard in many functions. Iterating over a list is unrelated to higher-order function status.

  3. Function Returning Function

    What makes the following a higher-order function: function multiplier(factor) { return function(x) { return x * factor; } }?

    1. It has nested functions.
    2. It returns a function as its result.
    3. It uses the factor variable.
    4. It multiplies numbers.

    Explanation: The key aspect is that multiplier returns a new function, making it higher-order. Multiplying numbers is merely the operation performed by the returned function. Having nested functions alone does not guarantee a higher-order function. The factor variable's usage is incidental to the higher-order property.

  4. Function Composition Definition

    What does function composition generally mean in the context of programming?

    1. Creating multiple variables in a function.
    2. Combining two or more functions so that the output of one becomes the input of another.
    3. Writing long functions.
    4. Separating functions into individual files.

    Explanation: Function composition involves linking functions so outputs feed directly as inputs, enabling building complex operations from simpler ones. Writing long functions is unrelated. Creating variables doesn’t imply composition, nor does splitting code across files.

  5. Identifying map as Higher-Order Function

    Why is the map function commonly considered a higher-order function?

    1. Because it creates a copy of an array.
    2. Because it always returns a boolean.
    3. Because it accepts a function as an argument to operate on each element of a list.
    4. Because it can only be used with arrays.

    Explanation: map’s higher-order status comes from accepting a function to apply to each item. Its use is not exclusive to arrays in all languages. Creating a copy is a result of mapping, not why it’s higher-order. Returning booleans is not guaranteed or required.

  6. Understanding filter Functionality

    What is the role of the callback function passed to a filter function in most programming languages?

    1. It sorts the elements.
    2. It modifies every element in the list.
    3. It determines whether each element should be included in the result.
    4. It prints each element to the screen.

    Explanation: The callback for filter is used to decide if an element stays in the output based on a condition, usually returning true or false. Modifying elements is typically achieved with map, while sorting and printing are different operations not related to filtering.

  7. Order of Operations in Composition

    If you compose functions f and g as f(g(x)), which function is applied first?

    1. f is applied first, then g.
    2. Neither function is applied.
    3. Both are applied at the same time.
    4. g is applied first, then f.

    Explanation: Composition f(g(x)) means you start by applying g to x, followed by f to the result. If f were first, the notation would be reversed. The functions are not executed simultaneously. Not applying either is not part of composition.

  8. Chaining Methods Example

    Given numbers.filter(isEven).map(double), what concept is demonstrated here?

    1. Recursion on an array.
    2. Defining a global variable.
    3. A loop inside a function.
    4. Function composition, where the output of one higher-order function feeds into another.

    Explanation: This statement demonstrates function composition by passing results from filter to map, chaining two higher-order functions. It does not involve explicit loops or recursion. No global variable is defined in this example.

  9. Pure Functions and Composition

    Why are pure functions preferred when using function composition?

    1. Because they require user input.
    2. Because they execute faster than impure functions.
    3. Because they have more parameters.
    4. Because they always produce the same output for the same input without side effects.

    Explanation: Pure functions make compositions predictable and reliable since they have no side effects, and outputs depend solely on inputs. Execution speed is not guaranteed to be higher. Parameter count does not influence purity. User input is unrelated to function purity.

  10. Anonymous Functions Usage

    When using a higher-order function like map, why might a programmer use an anonymous function as an argument?

    1. To create a short, one-time operation without defining a separate named function.
    2. Because anonymous functions are faster.
    3. To make code harder to read.
    4. Because named functions cannot be used.

    Explanation: Anonymous functions are often used when a unique operation is needed just once, making code concise. Performance is typically unchanged by anonymity. The intent is to improve, not reduce, readability. Named functions are allowed and may sometimes be preferable for clarity.