Functional Programming Essentials in Kotlin: Lambdas, Higher-Order Functions, and Collections Quiz

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.

  1. Understanding Lambdas in Kotlin

    Which of the following represents the correct syntax to declare a lambda in Kotlin that takes two integers and returns their sum?

    1. (a, b) -u003E a + b
    2. { a: Int, b: Int -u003E a + b }
    3. { a: Int; b: Int =u003E a + b }
    4. a: Int, b: Int =u003E a + b

    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.

  2. Identifying Higher-Order Functions

    What is a higher-order function in Kotlin?

    1. A function that takes another function as a parameter or returns a function
    2. A function that only works with numbers
    3. A function defined outside of any class
    4. A function written with uppercase letters

    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.

  3. Using the 'map' Function

    In Kotlin, what does the 'map' function do when used with a list of integers, such as listOf(1, 2, 3).map { it * 2 }?

    1. It creates a new list with each element doubled
    2. It sorts the list in ascending order
    3. It removes duplicate elements from the list
    4. It filters out all elements less than 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.

  4. Understanding Implicit 'it' Keyword

    What does the 'it' keyword represent inside a single-parameter lambda expression in Kotlin, such as in list.filter { it u003E 5 }?

    1. It is used to import libraries
    2. It is a reserved word for loop counters
    3. It represents the whole list
    4. It refers to the single parameter passed into the lambda

    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.

  5. Difference Between 'filter' and 'find'

    What distinguishes the 'filter' function from the 'find' function when working with collections in Kotlin?

    1. 'filter' removes duplicates, while 'find' splits the list
    2. 'filter' sorts the list, while 'find' reverses it
    3. 'filter' always returns an empty list, while 'find' throws an exception
    4. 'filter' returns a list of all matching elements, while 'find' returns the first matching element or null

    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.

  6. Using Function Types in Declarations

    Which of the following correctly defines a variable in Kotlin that can store a function accepting two Int parameters and returning Int?

    1. var sumFunction = Int, Int -u003E Int
    2. val sum: (Int Int) returns Int
    3. val sumFunction: Int -u003E Int -u003E Int
    4. val sum: (Int, Int) -u003E 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.

  7. Inline Functions for Lambda Optimization

    What is the main benefit of marking a higher-order function as 'inline' in Kotlin?

    1. It enforces type safety for generics
    2. It reduces the overhead of function calls when using lambdas
    3. It makes the function private to the file
    4. It increases the function's runtime speed regardless of usage

    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.

  8. Simplifying Collection Processing

    Which Kotlin function should you use to check if all elements of a list satisfy a condition, such as all numbers being positive?

    1. groupBy
    2. forEach
    3. none
    4. all

    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.

  9. Lambdas With Receivers

    What is one characteristic of a lambda with a receiver in Kotlin, such as when using the 'apply' function?

    1. The lambda must always return Boolean
    2. The lambda is always passed as a named argument
    3. The lambda can access the receiver object's members directly using 'this'
    4. The lambda cannot accept any parameters

    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.

  10. Combining Collection Functions

    If you want to filter even numbers and then compute their sum from a list in Kotlin, which combination should you use?

    1. map { it * 2 } followed by size
    2. find { it % 2 == 0 } followed by count()
    3. filter { it % 2 == 0 } followed by sum()
    4. toString() followed by filter

    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.