Kotlin Kotlin Functional Programming 2 — Questions and Answers
Question 1: What is the `also` scope function used for in Kotlin?
- Transforming an object and returning the result
- Performing side effects on an object and returning the object itself (Correct answer)
- Replacing the object with a new value
- Executing a block only if non-null
Correct answer: Performing side effects on an object and returning the object itself
`also` executes a block with the object as `it` for side effects (like logging) and returns the original object unchanged.
Question 2: What is the difference between `apply` and `also` in Kotlin?
- No difference
- `apply` uses `this` and returns the object; `also` uses `it` and returns the object (Correct answer)
- `apply` returns a transformed result; `also` returns the original
- `apply` is for nullables; `also` is for non-nullables
Correct answer: `apply` uses `this` and returns the object; `also` uses `it` and returns the object
Both `apply` and `also` return the receiver object, but `apply` exposes it as `this` for configuration, while `also` exposes it as `it` for side effects.
Question 3: What does `run` scope function do when called on an object in Kotlin?
- Starts a coroutine
- Calls a lambda with the object as `this` and returns the lambda result (Correct answer)
- Returns the object unchanged
- Prints the object to console
Correct answer: Calls a lambda with the object as `this` and returns the lambda result
`run` calls the block with the receiver as `this` and returns the result of the block, useful for transforming objects.
Question 4: What is tail recursion in Kotlin and how is it enabled?
- Recursion using a list as a stack
- Recursive calls optimized by the compiler using the `tailrec` modifier to avoid stack overflow (Correct answer)
- Calling a function from the end of another function
- Recursion with memoization
Correct answer: Recursive calls optimized by the compiler using the `tailrec` modifier to avoid stack overflow
The `tailrec` modifier tells the Kotlin compiler to optimize a recursive function into a loop, preventing stack overflow for deep recursion.
Question 5: What is a function type in Kotlin?
- A type that describes a class method
- A type that represents a function signature, e.g., (Int) -> String (Correct answer)
- A generic type parameter
- An interface with one method
Correct answer: A type that represents a function signature, e.g., (Int) -> String
Kotlin represents functions as first-class types using the notation `(ParameterTypes) -> ReturnType`, enabling functions to be stored and passed as values.
Question 6: What does `with` scope function do in Kotlin?
- Creates a new scope with imports
- Calls a lambda with an object as `this` and returns the lambda result, without being an extension function (Correct answer)
- Wraps an object in a nullable container
- Executes code only if the object is non-null
Correct answer: Calls a lambda with an object as `this` and returns the lambda result, without being an extension function
`with` is a non-extension function that takes an object as a parameter, calls the lambda with it as `this`, and returns the lambda result.
What is the `also` scope function used for in Kotlin?