menu

Kotlin Functions


1. What is the return type of a function that does not return a value in Kotlin?

Int

Unit

Void

Nothing


2. What is the purpose of the 'crossinline' modifier in a function declaration in Kotlin?

It allows the function to be called using infix notation

It specifies that the function should not have a return value

It specifies that the function should not have a receiver parameter

It prevents the use of non-local returns in the function body


3. What is the purpose of the 'infix' modifier in a function declaration in Kotlin?

It allows the function to be called with or without argument names

It allows the function to be called using infix notation

It allows the function to be called with a variable number of arguments

It allows the function to be called using prefix notation


4. What is the purpose of the 'inline' modifier in a function declaration in Kotlin?

It allows the function to be called using infix notation

It allows the function to be inlined at the call site to improve performance

It allows the function to be called with or without argument names

It allows the function to be called with a variable number of arguments


5. Which of the following is a valid way to declare a higher-order function in Kotlin?

fun sum(x: Int, y: Int, op: (Int, Int) -> Int): Int = op(x, y)

fun sum(x: Int, y: Int, op: Int, Int -> Int): Int = op(x, y)

fun sum(x: Int, y: Int, op: Int.(Int) -> Int): Int = x.op(y)

fun sum(x: Int, y: Int, op: (Int) -> Int): Int = op(x + y)


6. What is the purpose of the 'tailrec' modifier in a recursive function in Kotlin?

It allows the function to be called using tail recursion optimization

It allows the function to be called with a variable number of arguments

It allows the function to be called with or without argument names

It allows the function to be called using infix notation


7. What is the return type of a function in Kotlin if it does not return any value?

Int

Unit

Void

None


8. Which of the following is not a valid way to define a function with a variable number of arguments in Kotlin?

fun sum(vararg nums: Int): Int = nums.sum()

fun sum(nums: Array): Int = nums.sum()

fun sum(nums: List): Int = nums.sum()

fun sum(nums: Set): Int = nums.sum()


9. What is the purpose of the 'operator' modifier in a function declaration in Kotlin?

It allows the function to be called using infix notation

It allows the function to be inlined at the call site to improve performance

It allows the function to be called with a variable number of arguments

It allows the function to be used as an overloaded operator


10. Which of the following is a valid way to declare and call an anonymous function in Kotlin?

{ x: Int, y: Int -> x + y } (1, 2)

fun(x: Int, y: Int): Int { return x + y }(1, 2)

fun(x: Int, y: Int) = x + y (1, 2)

{ x, y -> x + y }(1, 2)