menu

Kotlin Basic Programs for Beginners


1.

What is the output of the following Kotlin code?

fun main() {
val str = "hello, world"
val result = str.split(" ")
println(result)
}

["hello,", "world"]

[hello,, world]

[hello, world]

Compilation error


2.

What is the output of the following Kotlin code?

fun main() {
val nums = arrayOf(1, 2, 3, 4, 5)
val result = nums.any { it % 2 == 0 }
println(result)
}

1

0

2

Compilation error


3.

What is the output of the following Kotlin code?

fun main() {
val nums = arrayOf(1, 2, 3, 4, 5)
val result = nums.sum()
println(result)
}

10

15

20

Compilation error


4.

What is the output of the following Kotlin code?

fun main() {
val nums = arrayOf(1, 2, 3, 4, 5)
val result = nums.map { it * 2 }
println(result)
}

[1, 2, 3, 4, 5]

[2, 4, 6, 8, 10]

[2, 4, 6, 8, 10, 12]

Compilation error


5.

What is the output of the following Kotlin code?

fun main() {
val str = "hello, world"
val result = str.filter { it.isLetter() }
println(result)
}

helloworld

hello,world

hello world

Compilation error


6.

What is the output of the following Kotlin code?

fun main() {
val num1: Int? = null
val num2: Int = 5
println(num1 ?: num2)
}

null

5

Compilation error

Runtime error


7.

What is the output of the following Kotlin code?

fun main() {
val nums = arrayOf(1, 2, 3, 4, 5)
val result = nums.last { it % 2 == 0 }
println(result)
}

2

4

null

Compilation error


8.

What is the output of the following Kotlin code?

fun main() {
val nums = arrayOf(1, 2, 3, 4, 5)
val result = nums.find { it % 2 == 0 }
println(result)
}

2

4

null

Compilation error


9.

What is the output of the following Kotlin code?

fun main() {
val nums = arrayOf(1, 2, 3, 4, 5)
val result = nums.max()
println(result)
}

1

5

null

Compilation error


10.

What is the output of the following Kotlin code?

fun main() {
val nums = arrayOf(1, 2, 3, 4, 5)
val sum = nums.reduce { acc, num -> acc + num }
println(sum)
}

1

15

30

Compilation error