0
0
Kotlinprogramming~10 mins

Run 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 run the block and return its result.

Kotlin
val result = run [1] {
    5 + 3
}
println(result)
Drag options to blanks, or click blank then click option'
A() -> Int
B{ Int -> Int }
C() -> Unit
DInt
Attempts:
3 left
💡 Hint
Common Mistakes
Using a lambda type with parameters instead of none.
Confusing the return type with the lambda parameter type.
2fill in blank
medium

Complete the code to run a block on a nullable object safely.

Kotlin
val name: String? = "Kotlin"
val length = name?.run [1] {
    length
} ?: 0
println(length)
Drag options to blanks, or click blank then click option'
A() -> Int
B{ String -> Int }
C() -> String
D{ Int -> Int }
Attempts:
3 left
💡 Hint
Common Mistakes
Adding a parameter to the lambda when using run on an object.
Not handling the nullable case with ?: 0.
3fill in blank
hard

Fix the error in the run block to correctly return the last character.

Kotlin
val word = "hello"
val lastChar = run [1] {
    word[word.length]
}
println(lastChar)
Drag options to blanks, or click blank then click option'
A() -> Int
B() -> String
C() -> Char
D() -> Unit
Attempts:
3 left
💡 Hint
Common Mistakes
Using an incorrect lambda type with parameters.
Confusing the return type with the parameter type.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths using run.

Kotlin
val words = listOf("apple", "banana", "cherry")
val lengths = words.associateWith [1] {
    it.[2]
}
println(lengths)
Drag options to blanks, or click blank then click option'
Arun
Blength
Ccount
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using count or size instead of length for strings.
Not using run in the first blank.
5fill in blank
hard

Fill all three blanks to filter and map words using run with conditions.

Kotlin
val words = listOf("dog", "cat", "elephant", "fox")
val filtered = words.filter [1] {
    it.length [2] 3
}.map [3] {
    it.uppercase()
}
println(filtered)
Drag options to blanks, or click blank then click option'
Arun
B>
Cfilter
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter or map as the lambda instead of run.
Using the wrong comparison operator.