0
0
Kotlinprogramming~10 mins

Platform types and null safety 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 '!' or '!!' which are not used for declaring nullable types.
Leaving the type without '?' which means it cannot hold null.
2fill in blank
medium

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

Kotlin
val length = name[1].length ?: 0
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 the value is null.
Using '?' alone which is not a valid operator.
Using ':' which is not an operator for property access.
3fill in blank
hard

Fix the error in the code by choosing the correct operator to assert non-null.

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 '?' or '?.' which allow null but do not assert non-null.
Using ':' which is not an operator here.
4fill in blank
hard

Fill both blanks to create a platform type variable and safely access its length.

Kotlin
val platformName: String[1] = getJavaString()
val length = platformName[2].length ?: 0
Drag options to blanks, or click blank then click option'
A?
B!!
C?.
D(no suffix)
Attempts:
3 left
💡 Hint
Common Mistakes
Adding '?' to platform type which is not needed.
Using '!!' which can cause exceptions if null.
5fill in blank
hard

Fill all three blanks to filter a list of platform type Strings for non-null values and get their lengths.

Kotlin
val javaList: List<String[1]> = getJavaList()
val lengths = javaList.filterNotNull().map { it[2].length }.filter { it [3] 0 }
Drag options to blanks, or click blank then click option'
A?
B!!
C>
D(no suffix)
Attempts:
3 left
💡 Hint
Common Mistakes
Adding '?' to platform type list.
Not asserting non-null before accessing length.
Using wrong comparison operator in filter.