0
0
Kotlinprogramming~10 mins

Also function behavior and use cases in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to use the also function to print the number before returning it.

Kotlin
fun printNumber(num: Int): Int {
    return num.[1] {
        println("Number is: $it")
    }
}
Drag options to blanks, or click blank then click option'
Aapply
Brun
Calso
Dlet
Attempts:
3 left
💡 Hint
Common Mistakes
Using apply which returns the object but uses this instead of it.
Using let which returns the lambda result, not the original object.
2fill in blank
medium

Complete the code to add an element to the list and print the updated list using also.

Kotlin
val numbers = mutableListOf(1, 2, 3)
numbers.[1](4).also {
    println("Updated list: $numbers")
}
Drag options to blanks, or click blank then click option'
Aalso
Brun
Clet
Dapply
Attempts:
3 left
💡 Hint
Common Mistakes
Using apply which expects this and is used for configuring objects.
Using let which returns the lambda result, not the original object.
3fill in blank
hard

Fix the error in the code by choosing the correct function to chain calls and print the object.

Kotlin
val text = "Hello"
val result = text.[1] {
    println(it)
}.uppercase()
Drag options to blanks, or click blank then click option'
Aapply
Balso
Crun
Dwith
Attempts:
3 left
💡 Hint
Common Mistakes
Using run which returns the lambda result, breaking the chain.
Using apply which uses this and may confuse chaining.
4fill in blank
hard

Fill both blanks to create a mutable list, add an element, and print the list using also.

Kotlin
val list = mutableListOf(1, 2, 3).[1] {
    this.add(4)
}.[2] {
    println(it)
}
Drag options to blanks, or click blank then click option'
Aalso
Blet
Capply
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using let which returns the lambda result, not the object.
Using run which returns the lambda result.
5fill in blank
hard

Fill all three blanks to create a string, print it, convert to uppercase, and print again using also.

Kotlin
val message = "hello".[1] {
    println("Original: $it")
}.[2]().[3] {
    println("Uppercase: $it")
}
Drag options to blanks, or click blank then click option'
Aalso
Blet
Cuppercase
Dapply
Attempts:
3 left
💡 Hint
Common Mistakes
Using let which changes the return value and breaks chaining.
Using apply which uses this and is less clear here.