Explore the intricacies of advanced function design patterns with this challenging quiz, covering closures, currying, higher-order functions, and related topics. Designed for those familiar with intermediate programming concepts, this quiz helps reinforce practical understanding of functional programming techniques.
Which statement best explains how a closure retains access to variables from its surrounding scope, as seen in a function returning another function that uses a local variable?
Explanation: Closures work by capturing and retaining access to the lexical environment in which they are defined, meaning the returned function can use variables from its outer function even after the outer function has completed. Option B is incorrect because closures do not lose reference to local variables. Option C incorrectly states that variables are copied as arguments, which is not how closures function. Option D is incorrect because closures specifically allow continued access to local, not just global, variables.
Given a function that accepts another function as an argument to map over a list of numbers, which design pattern is this an example of?
Explanation: A higher-order function is any function that takes one or more functions as parameters or returns a function as its result; mapping over a list using a provided function is a classic application. Tail recursion refers to a specific recursion optimization and does not involve passing functions as arguments. Memoization is about caching results and is unrelated to direct function arguments. An immutable update refers to copying data structures, not accepting a function as input.
If you transform a function that adds two numbers, add(x, y), into addCurried(x)(y), what is this pattern known as?
Explanation: Currying is the technique of converting a function with multiple arguments into a sequence of functions, each taking a single argument, as in transforming add(x, y) into addCurried(x)(y). Partial execution is a related but distinct concept where some arguments are pre-filled, not necessarily creating a sequence. The decorator pattern involves enhancing functionality, not argument transformation. Async chaining refers to handling asynchronous operations and is unrelated to function argument handling.
Which of the following best describes function composition using two functions, such as f(g(x)), in function design patterns?
Explanation: Function composition is about chaining functions so that the output of one becomes the input for the next, as in f(g(x)). Option B is incorrect as it suggests isolation instead of composition. Option C refers to wrapping functions as objects, unrelated to composing their calls. Option D describes a parallel application rather than sequential composition where input flows through each function.
When using memoization in a function that calculates Fibonacci numbers, what is the primary purpose of this pattern?
Explanation: Memoization is a technique for improving performance by caching the results of expensive function calls, so if the same input occurs, the function returns the cached result instead of recalculating. Recursion (option B) is often used in Fibonacci calculations but is not synonymous with memoization. Option C is about variable scope, not memoization. Option D describes pure functions but does not specifically address memoization.