Explore key computer science concepts with this quiz focusing on pure functions, immutability, and lazy evaluation. Assess your understanding of these foundational ideas essential for functional programming and efficient, predictable code.
Which statement best describes a pure function using the example of adding two numbers together?
Explanation: A pure function consistently returns the same result for the same arguments and has no side effects, like modifying external variables. Modifying a global variable or printing to the console introduces side effects and is not pure. Returning a random number makes the output unpredictable and dependent on more than just the inputs, so it is not pure either.
In the context of immutability, what happens if you attempt to modify an immutable data structure like a string or a tuple?
Explanation: When modifying immutable data structures, a new copy reflecting the change is produced, and the original remains unchanged. Changing the original in place is only possible with mutable types. Program crashes do not typically happen in such cases, and only updating the last element is unrelated to immutability.
Which best explains the concept of lazy evaluation in programming?
Explanation: Lazy evaluation means that a value or expression is computed only when it is required by the program, often increasing efficiency. Calculating all values at the beginning uses eager evaluation, not lazy. Avoiding all computations is impossible as some are necessary, and immediately removing values from memory is not a feature of lazy evaluation.
Is a function that sorts a list but does not modify the original list considered a pure function?
Explanation: A pure function must not change its inputs and should produce the same output for the same input. If the original list remains untouched and the result relies only on its input, the function is pure. Sorting itself is not inherently impure; it depends on implementation. Not returning a value would typically mean the function has no useful output, and just saying 'any sorting is pure' is incorrect.
Why are immutable objects often considered safer for multi-threaded programs?
Explanation: Immutability ensures that once an object is created, it cannot be modified, so no thread can alter its contents. This reduces risks from concurrent modifications. While immutable objects can help with performance, they are not always faster. Their type is still known to threads, and immutability does not provide automatic synchronization.
Given a function that generates an infinite sequence of numbers but only processes the first ten when needed, which concept is being used?
Explanation: Generating values from an infinite sequence only as they are needed is an example of lazy evaluation, allowing efficient use of resources. Mutable data structures are unrelated to generating data on demand. Strict evaluation would compute the entire sequence, which is not practical for infinite data. 'Pure side effects' is not a standard concept in this context.
Which of the following is an example of a side effect that makes a function impure?
Explanation: Writing to a log file is a side effect because it changes something outside the function. Returning calculated values and receiving parameters are core parts of functional programming and do not introduce side effects. Merely calculating or returning math expressions does not affect external state.
How does using immutable data structures typically affect the debugging of software?
Explanation: With immutability, data cannot be altered after it is created, which means changes are explicit and easier to track during debugging. This does not make debugging impossible; in fact, it can simplify debugging. Immutability leads to fewer unexpected changes, not more, and variable types remain visible during debugging.
If a function accesses and modifies a global variable each time it is called, is it still a pure function?
Explanation: A pure function must not produce or rely on side effects, and modifying or even reading a global variable counts as such. Simply having a return value does not guarantee purity. Not changing the variable still constitutes relying on outside state. The value of the global variable does not permit side effects in pure functions.
How does lazy evaluation differ from eager (strict) evaluation when processing a list of values?
Explanation: Lazy evaluation postpones computation until a value is needed, increasing efficiency in some cases. Eager evaluation computes everything upfront, regardless of necessity. Lazy evaluation does not calculate twice for accuracy and fully supports parameters. Eager evaluation does not ignore values; it computes all, even if unused.