menu

Kotlin Functions


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


2. What is the keyword used to define a function in Kotlin?

fun

function

define

declare


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


4. What is a lambda expression in Kotlin?

A named function

A higher-order function

An anonymous function

A function with a single expression


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

fun noArgs()

fun noArgs(): Unit

fun noArgs(): Void

fun noArgs(): Nothing


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


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

{ x: Int, y: Int -> x + y }

{ x: Int -> x * x }

{ x -> x * x }

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


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


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


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