0
0
Kotlinprogramming~10 mins

Chaining scope functions 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 chain two scope functions on the string.

Kotlin
val result = "hello".[1] {
    println(it.uppercase())
}
Drag options to blanks, or click blank then click option'
Aalso
Blet
Capply
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'let' returns the block result, not the original object.
Using 'apply' changes the context but returns the object, but it expects 'this' as receiver.
2fill in blank
medium

Complete the code to chain let and run to transform and print a string.

Kotlin
val length = "kotlin".let {
    it.uppercase()
}.[1] {
    println(this)
    this.length
}
Drag options to blanks, or click blank then click option'
Arun
Bapply
Cwith
Dalso
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'also' returns the original object, not the block result.
Using 'apply' returns the object, not the block result.
3fill in blank
hard

Fix the error in chaining apply and let to modify and print a mutable list.

Kotlin
val list = mutableListOf(1, 2, 3).[1] {
    add(4)
}.let {
    println(it)
    it.size
}
Drag options to blanks, or click blank then click option'
Aalso
Bapply
Crun
Dwith
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'also' returns the object but expects 'it' as context, which may confuse modification.
Using 'run' returns the block result, not the object.
4fill in blank
hard

Fill both blanks to chain let and also to transform and print a string.

Kotlin
val result = "scope".[1] {
    it.uppercase()
}.[2] {
    println(it)
}
Drag options to blanks, or click blank then click option'
Alet
Balso
Capply
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'apply' instead of 'also' changes the context to 'this' which is not expected here.
Using 'run' returns the block result but changes context to 'this'.
5fill in blank
hard

Fill all three blanks to chain apply, let, and also to modify, transform, and print a list.

Kotlin
val size = mutableListOf(5, 6).[1] {
    add(7)
}.[2] {
    it.sum()
}.[3] {
    println(it)
}
Drag options to blanks, or click blank then click option'
Aapply
Blet
Calso
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'run' changes context to 'this' and returns block result, which may confuse the chain.
Mixing 'also' and 'apply' in wrong order breaks the expected return values.