menu

Kotlin Functions


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


2. Which of the following is not a valid function declaration in Kotlin?

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

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

fun add(x: Int, y: Int): Unit = println(x + y)

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


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

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

sum(1, 2) { x -> x * x }

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

sum(1, 2, { x -> x * x })


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


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

fun

function

define

declare


6. What is a default argument in Kotlin?

An argument that is required to be passed to a function

An argument that is passed using a named parameter

An argument that has a default value specified in the function declaration

An argument that is passed using a lambda expression


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


8. Which of the following is a valid way to declare an extension function in Kotlin?

fun String.capitalize(): String = this.toUpperCase()

fun capitalize(String str): String = str.toUpperCase()

fun String.capitalize(): String = toUpperCase()

fun capitalize(str: String): String = str.toUpperCase()


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


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)