menu

Kotlin Basic Programs for Beginners

Kotlin Basic Programs for Beginners - Important Points


6.

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

A. [1, 2, 3, 4, 5]

B. [2, 4, 6, 8, 10]

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

D. Compilation error

Discuss Work Space

Answer: option b

Explanation:

Here, the map function is used to create a new array called result, which contains each element of the nums array multiplied by 2. Therefore, the output will be "[2, 4, 6, 8, 10]".

7.

What is the output of the following Kotlin code?

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

A. [1, 3, 5]

B. [2, 4]

C. [1, 2, 3, 4, 5]

D. Compilation error

Discuss Work Space

Answer: option b

Explanation:

Here, the filter function is used to create a new array called result, which contains only the even numbers from the nums array. Therefore, the output will be "[2, 4]".

8.

What is the output of the following Kotlin code?

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

A. ["hello,", "world"]

B. [hello,, world]

C. [hello, world]

D. Compilation error

Discuss Work Space

Answer: option b

Explanation:

Here, the split function is used to split the string str into an array of substrings, using the space character as the delimiter. Therefore, the output will be "[hello,", "world]".

9.

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

A. 1

B. 15

C. 30

D. Compilation error

Discuss Work Space

Answer: option b

Explanation:

Here, the reduce function is used to calculate the sum of all the elements in the nums array. The lambda function passed to reduce takes two arguments: the accumulator (acc) and the current element (num). The lambda function adds num to acc and returns the result, which becomes the new value of acc in the next iteration. Therefore, the output will be 15 (the sum of all the elements in the nums array).

10.

What is the output of the following Kotlin code?

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

A. 1

B. 15

C. 30

D. Compilation error

Discuss Work Space

Answer: option b

Explanation:

Here, the fold function is used to calculate the sum of all the elements in the nums array. The first argument to fold is the initial value of the accumulator (acc), which is 0 in this case. The lambda function passed to fold is similar to the one passed to reduce. Therefore, the output will be 15 (the sum of all the elements in the nums array).


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