Challenge - 5 Problems
Platform Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Understanding platform types and null safety
What is the output of this Kotlin code when calling a Java method that returns a platform type?
Kotlin
fun main() { val javaString: String = getJavaString() println(javaString.length) } // Simulated Java method returning platform type fun getJavaString(): String? = null
Attempts:
2 left
💡 Hint
Platform types allow nulls but Kotlin treats them as non-null by default.
✗ Incorrect
The Java method returns a platform type, which Kotlin treats as non-nullable. Assigning null to a non-nullable variable compiles but causes a NullPointerException at runtime when accessing its members.
❓ Predict Output
intermediate2:00remaining
Safe call operator with platform types
Given a platform type from Java, what will this Kotlin code print?
Kotlin
fun main() { val javaString = getJavaString() println(javaString?.length ?: "null length") } fun getJavaString(): String? = null
Attempts:
2 left
💡 Hint
The safe call operator ?. prevents NullPointerException.
✗ Incorrect
Using ?. safely accesses length only if javaString is not null. Since javaString is null, the Elvis operator ?: returns "null length".
🔧 Debug
advanced2:00remaining
Identify the cause of NullPointerException with platform types
Why does this Kotlin code throw a NullPointerException at runtime?
Kotlin
fun main() { val javaList: List<String> = getJavaList() println(javaList.size) } fun getJavaList(): List<String>? = null
Attempts:
2 left
💡 Hint
Platform types come from Java and Kotlin assumes non-null but no guarantee.
✗ Incorrect
Platform types have no nullability info, so Kotlin treats them as non-nullable. If the Java method returns null, accessing members causes NullPointerException.
📝 Syntax
advanced2:00remaining
Correctly declaring a platform type variable
Which Kotlin declaration correctly represents a platform type variable from Java that might be null?
Attempts:
2 left
💡 Hint
Platform types are not explicitly declared in Kotlin code.
✗ Incorrect
Platform types are inferred by Kotlin when calling Java code. You cannot explicitly declare a variable as a platform type with '!'. Using 'val javaStr = getJavaString()' lets Kotlin infer the platform type.
🚀 Application
expert3:00remaining
Handling platform types safely in Kotlin
You receive a platform type String! from a Java method that might be null. Which Kotlin code snippet safely handles this value to avoid runtime exceptions?
Attempts:
2 left
💡 Hint
Convert platform type to nullable type to safely check for null.
✗ Incorrect
Assigning the platform type to a nullable String allows safe calls and null checks. Options A and C risk NullPointerException. Option D is invalid because ?: cannot be used directly on platform types without explicit nullability.