0
0
Kotlinprogramming~20 mins

Platform types from Java interop in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Platform Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of accessing a platform type without null check

Consider the following Kotlin code calling a Java method that returns a platform type. What will be the output?

Kotlin
fun main() {
    val javaString: String = getJavaString()
    println(javaString.length)
}

// Java method signature: public static String getJavaString() { return null; }
APrints 0
BThrows a NullPointerException
CPrints 'null'
DCompilation error due to null safety
Attempts:
2 left
💡 Hint

Platform types can be null but Kotlin does not enforce null checks on them.

🧠 Conceptual
intermediate
1:30remaining
Understanding platform types in Kotlin-Java interop

Which statement best describes platform types in Kotlin when calling Java code?

APlatform types are Kotlin types with unknown nullability, requiring caution.
BPlatform types are always treated as non-nullable in Kotlin.
CPlatform types are deprecated and should not be used.
DPlatform types enforce strict null checks at compile time.
Attempts:
2 left
💡 Hint

Think about how Kotlin handles nullability when it cannot be sure from Java.

🔧 Debug
advanced
2:30remaining
Identify the cause of NullPointerException with platform types

Given this Kotlin code calling a Java method, why does it throw a NullPointerException?

val result: String = getJavaString()
println(result.length)

Java method getJavaString() returns null.

AKotlin treats the Java return as non-nullable, so no null check is done.
BThe Kotlin compiler inserts a null check that fails at runtime.
CThe Java method signature declares the return as nullable, but Kotlin ignores it.
DThe Kotlin variable is declared nullable but accessed as non-nullable.
Attempts:
2 left
💡 Hint

Consider how Kotlin interprets Java types without nullability annotations.

📝 Syntax
advanced
1:30remaining
Correct Kotlin declaration for platform type usage

Which Kotlin declaration correctly handles a platform type from Java that might be null?

Aval name: String = getJavaName()
Bval name: String! = getJavaName()
Cval name: String? = getJavaName()
Dval name: String!! = getJavaName()
Attempts:
2 left
💡 Hint

Think about how to safely handle possible null values from Java.

🚀 Application
expert
3:00remaining
Handling platform types safely in Kotlin code

You have a Java method String getUserName() that may return null. How should you safely use it in Kotlin to avoid runtime exceptions?

A
val userName: String? = getUserName()
println(userName.length)
Bval userName: String = getUserName()
Cval userName: String = getUserName()!!
Dval userName: String = getUserName() ?: "Unknown"
Attempts:
2 left
💡 Hint

Consider Kotlin's null safety operators to provide a default value.