Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to safely print the length of the string using let.
Kotlin
val name: String? = "Kotlin" name[1] { println(it.length) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .let without safe call causes error if variable is null.
Using !!let is invalid syntax.
Using ?:let is incorrect because ?: is the Elvis operator.
✗ Incorrect
The safe call operator ?. is used before let to ensure the block runs only if 'name' is not null.
2fill in blank
mediumComplete the code to safely convert the 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .let without safe call causes NullPointerException if input is null.
Using !!let is invalid syntax.
Using ?:let is incorrect; ?: is the Elvis operator.
✗ Incorrect
Using ?.let ensures the uppercase conversion happens only if 'input' is not null; otherwise, "default" is used.
3fill in blank
hardFix the error in the code to safely print the first character of the string using let.
Kotlin
val text: String? = null
text[1] { println(it.first()) } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .let without safe call causes NullPointerException.
Using !!let is invalid syntax.
Using ?:let is incorrect.
✗ Incorrect
The safe call operator ?. before let prevents calling let on a null value, avoiding runtime errors.
4fill in blank
hardFill both blanks to safely print the length of the string if it is not null and longer than 3 characters.
Kotlin
val word: String? = "code" word[1] { if (it.length [2] 3) println(it.length) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .let without safe call causes error if word is null.
Using < instead of > changes the logic.
Using ?:let is invalid.
✗ Incorrect
Use ?.let to safely call let only if word is not null, and > to check if length is greater than 3.
5fill in blank
hardFill all three blanks to create a list of lengths only for words longer than 4 characters.
Kotlin
val words: List<String?> = listOf("apple", "bat", "carrot") val lengths = words.mapNotNull { it[1] { if (it.length [2] 4) it[3] { it.length } else null } } println(lengths)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .let without safe call causes errors if any word is null.
Using < instead of > changes the filter condition.
Misplacing .let inside the lambda.
✗ Incorrect
Use ?.let to safely call let on each word, > to check length, and .let inside the lambda to return length or null.