menu

Kotlin Basic Programs for Beginners

Kotlin Basic Programs for Beginners - Important Points


11.

What is the output of the following Kotlin code?

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

A. helloworld

B. hello,world

C. hello world

D. Compilation error

Discuss Work Space

Answer: option a

Explanation:

Here, the filter function is used to create a new string called result, which contains only the letters from the str string. The lambda function passed to filter checks if each character (it) is a letter using the isLetter function. Therefore, the output will be "helloworld".

12.

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)
}

A. 1

B. 0

C. 2

D. Compilation error

Discuss Work Space

Answer: option a

Explanation:

Here, the any function is used to check if any element in the nums array is even. The lambda function passed to any checks if each element (it) is even using the modulo operator (%) and the number 2. Since the array contains at least one even number (2), the output will be true.

13.

What is the output of the following Kotlin code?

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

A. 1

B. 0

C. 2

D. Compilation error

Discuss Work Space

Answer: option b

Explanation:

Here, the all function is used to check if all elements in the nums array are even. The lambda function passed to all checks if each element (it) is even using the modulo operator (%) and the number 2. Since the array contains at least one odd number (1), the output will be false.

14.

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)
}

A. 2

B. 4

C. null

D. Compilation error

Discuss Work Space

Answer: option a

Explanation:

Here, the find function is used to find the first element in the nums array that is even. The lambda function passed to find checks if each element (it) is even using the modulo operator (%) and the number 2. Since the first even number in the array is 2, the output will be 2.

15.

What is the output of the following Kotlin code?

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

A. 2

B. 4

C. null

D. Compilation error

Discuss Work Space

Answer: option a

Explanation:

Here, the first function is used to find the first element in the nums array that is even. The lambda function passed to first checks if each element (it) is even using the modulo operator (%) and the number 2. Since the first even number in the array is 2, the output will be 2.


Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now