0
0
Kotlinprogramming~10 mins

With 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 with function to print the length of the string.

Kotlin
val text = "Hello"
with([1]) {
    println(length)
}
Drag options to blanks, or click blank then click option'
Alength
B"Hello"
Ctext
Dprintln
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string literal instead of the variable.
Using a function name instead of the object.
2fill in blank
medium

Complete the code to use with to build a string and return its length.

Kotlin
val result = with(StringBuilder()) {
    append("Hi")
    append(" there")
    [1]
}
Drag options to blanks, or click blank then click option'
Alength
Bbuild()
CtoString()
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Calling toString() without getting length.
Using size which is not a StringBuilder property.
3fill in blank
hard

Fix the error in the code by completing the with block to correctly modify the list.

Kotlin
val numbers = mutableListOf(1, 2, 3)
with(numbers) {
    add(4)
    [1]
}
Drag options to blanks, or click blank then click option'
AaddAll(listOf(5, 6))
Bclear()
Cremove(1)
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove which deletes elements instead of adding.
Using clear which empties the list.
4fill in blank
hard

Fill both blanks to create a with block that modifies a StringBuilder and returns the final string.

Kotlin
val result = with(StringBuilder()) {
    [1]("Hello")
    [2](" World")
    toString()
}
Drag options to blanks, or click blank then click option'
Aappend
Binsert
Cdelete
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using insert which adds text at a specific position.
Using delete or replace which modify existing text.
5fill in blank
hard

Fill all three blanks to use with to configure a MutableList and return its size.

Kotlin
val list = mutableListOf<Int>()
val size = with(list) {
    [1](10)
    [2](20)
    [3]
}
Drag options to blanks, or click blank then click option'
Aadd
Csize
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using clear which empties the list.
Trying to use length which is not a list property.