menu

Kotlin Functions


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


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


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


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

Int

Unit

Void

None


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


6. Which of the following is a valid way to call a function with named arguments in Kotlin?

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

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

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

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


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


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

fun

function

define

declare


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. What is the return type of a function that does not return a value in Kotlin?

Int

Unit

Void

Nothing