Closures, Currying, and Partial Application Essentials Quiz Quiz

Assess your understanding of closures, currying, and partial application with beginner-friendly scenarios and examples. This quiz covers key concepts, behaviors, and real-world applications related to these important programming patterns.

  1. Understanding Closures

    In programming, what is the main feature that distinguishes a closure from a regular function?

    1. A closure can only accept one argument.
    2. A closure is the same as a pure function.
    3. A closure always returns a numeric value.
    4. A closure can access variables from its outer scope even after that scope has finished executing.

    Explanation: Closures are special because they 'remember' the environment in which they were created, allowing them to access variables even after the outer function has exited. The other options confuse closures with unrelated concepts: closures are not limited to numeric returns, one argument, or being pure functions. Pure functions and closures are different; closures maintain references to variables from their outer lexical environment.

  2. Simple Currying Concept

    Which statement best describes currying in functional programming?

    1. Currying transforms a function with multiple arguments into a sequence of functions, each taking a single argument.
    2. Currying turns a closure into a pure function.
    3. Currying completely removes the need for arguments.
    4. Currying always increases the number of arguments a function accepts.

    Explanation: Currying is a process that breaks down a function expecting multiple arguments into a series of unary (single-argument) functions. Currying does not make a function accept more arguments, nor does it turn closures into pure functions. Removing the need for arguments altogether is not what currying does; it restructures how arguments are received.

  3. Partial Application Basics

    When using partial application, what happens to a function?

    1. Some arguments of the function are fixed, creating a new function that expects the remaining arguments.
    2. The function is executed immediately with no arguments.
    3. All arguments of the function are removed.
    4. Partial application always returns the original function unchanged.

    Explanation: Partial application involves supplying some of a function's arguments up front, returning a new function that requires the remaining arguments. Removing all arguments or executing the function immediately does not describe partial application. The original function is not returned unchanged; instead, a new function with preset arguments is created.

  4. Closures in Action

    Given a function that returns another function accessing an outer variable, what programming concept does this illustrate?

    1. Closure
    2. Shadowing
    3. Hoisting
    4. Mutation

    Explanation: When a function returns another function that uses variables from the outer function's scope, it demonstrates a closure. Mutation involves changing values, shadowing is hiding an outer variable with a local one, and hoisting refers to declarations being processed before code execution. Only closure captures the described behavior.

  5. Identifying Curried Functions

    If a function is defined as f(a)(b), returning the sum of a and b, which term best describes f?

    1. Bound method
    2. Recursive function
    3. Deferred promise
    4. Curried function

    Explanation: A curried function is a series of functions each taking a single argument, as in f(a)(b). Bound methods relate to object contexts, recursion is about self-invocation, and deferred promises relate to asynchronous programming. The correct concept here is currying.

  6. Partial vs. Curry

    How does partial application differ from currying?

    1. Partial application always creates a closure, unlike currying.
    2. Partial application increases the number of expected arguments.
    3. Currying fixes arguments just like partial application.
    4. Partial application fixes some arguments at once, while currying transforms a function into a sequence of unary functions.

    Explanation: Partial application presets certain arguments, while currying changes the structure of the function to accept one argument at a time. Currying does not fix arguments; it restructures the way parameters are taken. Both can produce closures, but not exclusively, and neither partial application nor currying increases the function’s arity.

  7. Lexical Scope and Closures

    What allows a closure to remember and use variables even after their original context has finished executing?

    1. Hoisting
    2. Runtime polymorphism
    3. Dynamic typing
    4. Lexical scope

    Explanation: Closures use lexical scope, meaning they have access to variables defined in their outer (enclosing) scope even after their environment has exited. Dynamic typing refers to variable types, runtime polymorphism is about method overriding, and hoisting concerns declaration order. Only lexical scope enables closures to function properly.

  8. Practical Partial Application

    If you partially apply a multiply function with the first argument as 10, what does the new function do?

    1. Multiplies any given number by 10
    2. Adds 10 to any given number
    3. Divides 10 by any given number
    4. Returns always 10 regardless of the argument

    Explanation: By partially applying the multiply function with 10, each new call only requires the second argument and multiplies it by 10. The other options describe addition, returning a constant, or division, none of which occur if the original function is multiplication with one argument fixed.

  9. Closure Preservation

    When a closure is created in a loop, how can each created function keep a different value from the loop variable?

    1. By relying on automatic garbage collection
    2. By using only global variables
    3. By creating a new local scope inside the loop iteration, such as with a helper function
    4. By using the same reference to the loop variable

    Explanation: Encapsulating each iteration’s variable in a new scope (for instance, via a helper function or block-scoped variables) allows closures to capture different values. Using global variables leads to shared values, and garbage collection does not separate loop variable states. Simply referencing the same variable would make all closures see the final value after the loop.

  10. When to Use Currying

    Which situation is most suitable for using currying in your code design?

    1. When you want to create reusable functions that can be customized incrementally with specific arguments
    2. When you want to disable all function arguments permanently
    3. When you need to execute a function immediately after its declaration
    4. When you need a function to run multiple times in a background process

    Explanation: Currying is best used when you need functions that can be built up step by step, each time specifying or customizing one argument. Immediate execution is unrelated to currying, and disabling arguments or scheduling background tasks do not involve the currying concept. Currying increases flexibility and modularity in function application.