menu

Kotlin Functions


1. 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


2. 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()


3. 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 allows the function to be inlined at the call site to improve performance

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

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


4. What is a higher-order function in Kotlin?

A function that returns a value

A function that takes another function as a parameter or returns a function as a result

A function that has a variable number of arguments

A function that can be called without any arguments


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

Int

Unit

Void

None


6. How is a function that takes no arguments declared in Kotlin?

fun noArgs()

fun noArgs(): Unit

fun noArgs(): Void

fun noArgs(): Nothing


7. Which of the following is not a valid way to call a function in Kotlin?

add(1, 2)

add(x = 1, y = 2)

add(y = 2, x = 1)

x.add(2)


8. 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


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. 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