0
0
Kotlinprogramming~10 mins

Safe call operator (?.) 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 access the length of the string variable.

Kotlin
val length = name[1]length
Drag options to blanks, or click blank then click option'
A?:
B!!.
C?.
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using the regular dot operator (.) causes a crash if the object is null.
Using the not-null assertion (!!.) is unsafe and can throw exceptions.
2fill in blank
medium

Complete the code to safely call the function toUpperCase() on a nullable string.

Kotlin
val upper = text[1]toUpperCase()
Drag options to blanks, or click blank then click option'
A?:
B.
C!!.
D?.
Attempts:
3 left
💡 Hint
Common Mistakes
Using the regular dot operator (.) causes a null pointer exception if text is null.
Using the not-null assertion (!!.) can crash the program if text is null.
3fill in blank
hard

Fix the error in the code by safely accessing the property length of a nullable string.

Kotlin
val len = nullableString[1]length
Drag options to blanks, or click blank then click option'
A?.
B!!.
C.
D?:
Attempts:
3 left
💡 Hint
Common Mistakes
Using the regular dot operator (.) causes a null pointer exception.
Using the not-null assertion (!!.) can cause a crash if the value is null.
4fill in blank
hard

Fill both blanks to safely call substring on a nullable string and provide a default empty string if null.

Kotlin
val result = nullableStr[1]substring(0, 3) [2] ""
Drag options to blanks, or click blank then click option'
A?.
B?:
C!!.
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using the not-null assertion (!!.) instead of safe call.
Not providing a default value for null cases.
5fill in blank
hard

Fill all three blanks to safely access the length of a nullable string's trimmed version, or return 0 if null.

Kotlin
val length = nullableStr[1]trim()[2]length [3] 0
Drag options to blanks, or click blank then click option'
A?.
B?:
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using safe call before length unnecessarily.
Not providing a default value for null cases.