Explore the core principles of functional programming in Kotlin with this quiz focusing on lambdas, higher-order functions, and collection operations. Assess your understanding of Kotlin's concise syntax and efficient handling of data transformations using functional programming concepts.
Which of the following represents the correct syntax to declare a lambda in Kotlin that takes two integers and returns their sum?
Explanation: The correct lambda syntax in Kotlin uses curly braces, lists parameters with their types (separated by commas), and uses the '-u003E' symbol to separate parameters from the body. The option '{ a: Int, b: Int -u003E a + b }' matches this structure. Other options use incorrect symbols like ';' or '=u003E', or omit necessary keywords, making them invalid in Kotlin.
What is a higher-order function in Kotlin?
Explanation: Higher-order functions are those that either take functions as parameters or return functions. They are fundamental to functional programming. A function written in uppercase, restricted only to numbers, or simply defined outside a class is unrelated to the concept of higher-order functions.
In Kotlin, what does the 'map' function do when used with a list of integers, such as listOf(1, 2, 3).map { it * 2 }?
Explanation: The 'map' function applies the given lambda to each element and returns a new list with the results. In this example, each number is multiplied by two. Filtering, removing duplicates, or sorting the list are not the purpose of 'map'; those are functions like 'filter', 'distinct', or 'sorted' respectively.
What does the 'it' keyword represent inside a single-parameter lambda expression in Kotlin, such as in list.filter { it u003E 5 }?
Explanation: In Kotlin, 'it' is the default name for the single parameter in a lambda if no explicit name is provided. It doesn't refer to the list itself, is not a loop counter, and is unrelated to importing libraries. The other options misinterpret the purpose of 'it' in this context.
What distinguishes the 'filter' function from the 'find' function when working with collections in Kotlin?
Explanation: The 'filter' function returns a collection of elements that meet the condition, and 'find' returns the first element that matches or null if none is found. Sorting and reversing are the jobs of other collection functions, not 'filter' or 'find', and removing duplicates or splitting lists are handled by distinct or partition methods.
Which of the following correctly defines a variable in Kotlin that can store a function accepting two Int parameters and returning Int?
Explanation: The correct function type syntax in Kotlin is '(parameter types) -u003E return type', as in '(Int, Int) -u003E Int'. The second option uses an incorrect assignment and syntax; the third incorrectly nests types, and the fourth uses 'returns' which is not valid in Kotlin.
What is the main benefit of marking a higher-order function as 'inline' in Kotlin?
Explanation: Marking a higher-order function as 'inline' in Kotlin instructs the compiler to substitute the function’s code at the call site, reducing call overhead caused by lambdas. It does not change visibility, enforce generic type safety, or universally increase execution speed regardless of usage. Those are not related to inlining.
Which Kotlin function should you use to check if all elements of a list satisfy a condition, such as all numbers being positive?
Explanation: The 'all' function returns true if all elements satisfy the given predicate, which is useful for checks like all values being positive. 'none' checks if no elements satisfy the condition, 'groupBy' groups the collection, and 'forEach' is used for iterating, not for checking conditions.
What is one characteristic of a lambda with a receiver in Kotlin, such as when using the 'apply' function?
Explanation: Lambdas with receivers allow you to access the object’s members as if you were in the object itself, using 'this'. They can accept parameters and return any type, not just Boolean. There is no restriction on named arguments for lambdas with receivers.
If you want to filter even numbers and then compute their sum from a list in Kotlin, which combination should you use?
Explanation: First filtering for even numbers and then summing them is done using 'filter { it % 2 == 0 }' followed by 'sum()'. Mapping doubles the numbers, not filters them, and 'find' only returns the first match, not a list for summing. Using 'toString()' converts the list, making filtering numbers not possible.