0
0
Kotlinprogramming~10 mins

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

Kotlin
val name: String? = "Kotlin"
name?.[1] {
    println("Length: ${it.length}")
}
Drag options to blanks, or click blank then click option'
Alet
Brun
Capply
Dalso
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'run' instead of 'let' which doesn't pass the object as 'it' in this context.
Using 'apply' which uses 'this' instead of 'it'.
Using 'also' which returns the original object but is less common for this use case.
2fill in blank
medium

Complete the code to safely convert a nullable string to uppercase using let.

Kotlin
val input: String? = "hello"
val result = input?.[1] {
    it.uppercase()
} ?: "DEFAULT"
Drag options to blanks, or click blank then click option'
Aalso
Blet
Cwith
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'also' which returns the original object, not the block result.
Using 'run' without safe call operator, which can cause null pointer exceptions.
Using 'with' which is not an extension function and requires the object as argument.
3fill in blank
hard

Fix the error in the code by choosing the correct function to chain calls and return the last expression.

Kotlin
val number = 5
val result = number.[1] {
    println("Number is $it")
    it * 2
}
println(result)
Drag options to blanks, or click blank then click option'
Aapply
Brun
Calso
Dlet
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'also' which returns the original object, so result would be 5, not 10.
Using 'apply' which uses 'this' and returns the original object.
Using 'run' which uses 'this' and is not called as extension here.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths, but only include words longer than 3 characters.

Kotlin
val words = listOf("cat", "house", "dog", "elephant")
val lengths = words.associateWith { it.[1] }
    .filter { it.value [2] 3 }
Drag options to blanks, or click blank then click option'
Alength
Bcount
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count' instead of 'length'; 'count' requires a predicate and does not return total length.
Using '<' instead of '>' which would filter shorter words instead.
Using 'count' without parentheses which is a function.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths, including only words longer than 4 characters.

Kotlin
val words = listOf("apple", "bat", "carrot", "dog")
val result = words.filter { it.length [1] 4 }
    .associateBy({ it.[2]() }, { it.[3] })
Drag options to blanks, or click blank then click option'
A>
Buppercase
Clength
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in filter condition.
Using 'lowercase' instead of 'uppercase' for keys.
Using 'length' without parentheses if it was a function (it's a property).