0
0
Kotlinprogramming~10 mins

Nullable types with ? suffix 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 declare a nullable String variable.

Kotlin
var name: String[1] = null
Drag options to blanks, or click blank then click option'
A?
B!
C(no suffix)
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using an exclamation mark instead of a question mark.
Leaving the type non-nullable and assigning null.
2fill in blank
medium

Complete the code to safely access the length of a nullable String.

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 !! which throws an exception if null.
Using just a dot which causes a compile error on nullable types.
3fill in blank
hard

Fix the error in the code to assign a nullable String to a non-nullable variable.

Kotlin
val nonNullName: String = name[1]
Drag options to blanks, or click blank then click option'
A!!
B?.toString()
C?
D?: ""
Attempts:
3 left
💡 Hint
Common Mistakes
Using safe call ?. which returns nullable type.
Using ?: "" which is for providing default values.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths only if length is greater than 3.

Kotlin
val lengths = mapOf([1] to [2]).filter { it.value > 3 }
Drag options to blanks, or click blank then click option'
A"hello"
B"hello".length
C"hi"
D"hi".length
Attempts:
3 left
💡 Hint
Common Mistakes
Using a word shorter than 4 characters which will be filtered out.
Using the length property incorrectly.
5fill in blank
hard

Fill all three blanks to create a nullable variable, check if it is null, and provide a default value.

Kotlin
val input: String[1] = null
val result = input [2] "default"
println(result[3])
Drag options to blanks, or click blank then click option'
A?
B?:
C?.length
D!!
Attempts:
3 left
💡 Hint
Common Mistakes
Not marking the variable as nullable.
Using !! which throws exception if null.
Not using safe call operator before accessing length.