0
0
Kotlinprogramming~10 mins

Let function with safe calls 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 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'
A.let
B?.let
C?:let
D!!let
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.
2fill in blank
medium

Complete 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'
A.let
B?:let
C!!let
D?.let
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.
3fill in blank
hard

Fix 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'
A.let
B!!let
C?.let
D?:let
Attempts:
3 left
💡 Hint
Common Mistakes
Using .let without safe call causes NullPointerException.
Using !!let is invalid syntax.
Using ?:let is incorrect.
4fill in blank
hard

Fill 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'
A?.let
B>
C<
D.let
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.
5fill in blank
hard

Fill 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'
A?.let
B>
C<
D.let
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.