menu

Kotlin Basic Programs for Beginners

Kotlin Basic Programs for Beginners - Important Points


16.

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

A. 2

B. 4

C. null

D. Compilation error

Discuss Work Space

Answer: option b

Explanation:

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

17.

What is the output of the following Kotlin code?

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

A. 1

B. 2

C. 3

D. Compilation error

Discuss Work Space

Answer: option b

Explanation:

Here, the count function is used to count the number of elements in the nums array that are even. The lambda function passed to count checks if each element (it) is even using the modulo operator (%) and the number 2. Since there are two even numbers in the array (2 and 4), the output will be 2.

18.

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

A. 1

B. 5

C. null

D. Compilation error

Discuss Work Space

Answer: option b

Explanation:

Here, the max function is used to find the maximum element in the nums array. Therefore, the output will be 5.

19.

What is the output of the following Kotlin code?

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

A. 1

B. 5

C. null

D. Compilation error

Discuss Work Space

Answer: option a

Explanation:

Here, the min function is used to find the minimum element in the nums array. Therefore, the output will be 1.

20.

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

A. 10

B. 15

C. 20

D. Compilation error

Discuss Work Space

Answer: option b

Explanation:

Here, the sum function is used to find the sum of all the elements in the nums array. Therefore, the output will be 15 (1 + 2 + 3 + 4 + 5).


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