Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to safely call a Java method that returns a platform type.
Kotlin
val length = javaString?.[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using length without parentheses causes an error.
✗ Incorrect
Java strings use length() method, so you must call it with parentheses.
2fill in blank
mediumComplete the code to safely handle a platform type that might be null.
Kotlin
val safeString: String = javaString[1] ?: "default"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
? or ?. does not assert non-null.✗ Incorrect
The !! operator asserts the value is not null, throwing an exception if it is.
3fill in blank
hardFix the error in calling a Java method returning a platform type that might be null.
Kotlin
val length = javaString.[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
length property causes error.Forgetting
!! leads to nullable type.✗ Incorrect
Calling length() and then asserting non-null with !! ensures safe usage.
4fill in blank
hardFill both blanks to safely check and use a platform type from Java.
Kotlin
if (javaString [1] null) { val length = javaString.[2]() }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using == null instead of != null.
Using property length instead of method length().
✗ Incorrect
Check if javaString is not null, then call length() method.
5fill in blank
hardFill all three blanks to create a safe call chain with platform types from Java.
Kotlin
val length = javaString[1]?.[2]()?.let { it [3] 0 } ?: -1
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using property length without parentheses.
Missing null assertion operator.
✗ Incorrect
Assert non-null with !!, call length(), then check if length is greater than zero.