Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is a platform type in Kotlin when interoperating with Java code?
easy
A. A type whose nullability is unknown and can be treated as nullable or non-nullable
B. A Kotlin type that is always non-nullable
C. A Java type that is always nullable
D. A Kotlin type that enforces strict null checks at compile time

Solution

  1. Step 1: Understand Kotlin-Java interoperability

    Kotlin calls Java code but Java does not specify nullability explicitly.
  2. Step 2: Define platform types

    Platform types are types from Java whose nullability Kotlin cannot be sure about, so Kotlin treats them flexibly as nullable or non-nullable.
  3. Final Answer:

    A type whose nullability is unknown and can be treated as nullable or non-nullable -> Option A
  4. Quick Check:

    Platform type = unknown nullability [OK]
Hint: Platform types have unknown nullability from Java [OK]
Common Mistakes:
  • Thinking platform types are always non-nullable
  • Assuming platform types enforce null checks
  • Confusing platform types with Kotlin nullable types
2. Which of the following Kotlin declarations correctly handles a Java platform type that might be null?
easy
A. val name: String!! = javaObject.getName()
B. val name: String = javaObject.getName()
C. val name: String? = javaObject.getName()
D. val name: String = javaObject.getName()!!

Solution

  1. Step 1: Recognize platform type usage

    Java method returns a platform type; Kotlin doesn't know if it's nullable.
  2. Step 2: Choose safe Kotlin type

    Declaring as nullable String? safely handles possible null values.
  3. Final Answer:

    val name: String? = javaObject.getName() -> Option C
  4. Quick Check:

    Use nullable type for platform types to avoid crashes [OK]
Hint: Use nullable type (String?) for uncertain Java values [OK]
Common Mistakes:
  • Using non-nullable type without null check
  • Using double-bang (!!) unnecessarily
  • Declaring invalid Kotlin types like String!!
3. Consider this Kotlin code calling a Java method returning a platform type:
val length = javaObject.getName().length

What happens if getName() returns null at runtime?
medium
A. The code throws a NullPointerException at runtime
B. The code fails to compile due to null safety
C. The code compiles and runs safely, length is 0
D. The code returns length as -1

Solution

  1. Step 1: Understand platform type nullability

    Platform types can be null but Kotlin treats them as non-nullable unless specified.
  2. Step 2: Analyze code behavior

    Calling length on a null value causes a NullPointerException at runtime.
  3. Final Answer:

    The code throws a NullPointerException at runtime -> Option A
  4. Quick Check:

    Platform types can cause runtime NPE if null used unsafely [OK]
Hint: Accessing platform type without null check risks NPE [OK]
Common Mistakes:
  • Assuming Kotlin prevents runtime null errors
  • Thinking length returns 0 for null
  • Expecting compile-time error for platform types
4. This Kotlin code calls a Java method returning a platform type:
val length = javaObject.getName()?.length ?: 0

What is the main benefit of using ?. here?
medium
A. It forces the compiler to treat the value as non-null
B. It throws an exception if the value is null
C. It converts the platform type to a non-nullable type
D. It safely handles null by returning 0 instead of crashing

Solution

  1. Step 1: Identify safe call operator usage

    The ?. operator calls length only if getName() is not null.
  2. Step 2: Understand Elvis operator fallback

    If getName() is null, the expression returns 0 instead of crashing.
  3. Final Answer:

    It safely handles null by returning 0 instead of crashing -> Option D
  4. Quick Check:

    Safe call + Elvis operator prevent null crashes [OK]
Hint: Use ?. and ?: to avoid null crashes with platform types [OK]
Common Mistakes:
  • Thinking ?. forces non-null type
  • Assuming it throws exception on null
  • Confusing safe call with non-null assertion (!!)
5. You have a Java method fun getUser(): User returning a platform type. You want to safely get the user's email or return "unknown" if null. Which Kotlin code correctly handles this?
hard
A. val email = getUser().email ?: "unknown"
B. val email = getUser()?.email ?: "unknown"
C. val email = getUser().email!!
D. val email = getUser().email

Solution

  1. Step 1: Recognize platform type and possible nulls

    getUser() returns a platform type; email might be null.
  2. Step 2: Use safe call and Elvis operator

    Using ?. safely accesses email; ?: provides fallback "unknown" if null.
  3. Final Answer:

    val email = getUser()?.email ?: "unknown" -> Option B
  4. Quick Check:

    Safe call + fallback handles null platform types safely [OK]
Hint: Use ?. with ?: to safely access nullable Java values [OK]
Common Mistakes:
  • Using non-null assertion (!!) risking runtime crash
  • Ignoring possible null from platform type
  • Not providing fallback for null values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard