Challenge - 5 Problems
Java-Kotlin Interop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of calling Java method from Kotlin
What is the output of this Kotlin code when calling a Java method?
Kotlin
public class JavaClass { public static String greet(String name) { return "Hello, " + name + "!"; } } fun main() { println(JavaClass.greet("Alice")) }
Attempts:
2 left
💡 Hint
Java static methods can be called directly from Kotlin using the class name.
✗ Incorrect
The Kotlin code calls the static Java method greet with argument "Alice". The method returns "Hello, Alice!" which is printed.
❓ Predict Output
intermediate2:00remaining
Accessing Java fields from Kotlin
What will be printed when this Kotlin code accesses a Java field?
Kotlin
public class JavaData { public int number = 42; } fun main() { val data = JavaData() println(data.number) }
Attempts:
2 left
💡 Hint
Public Java fields are accessible directly from Kotlin.
✗ Incorrect
The Kotlin code creates an instance of JavaData and prints the public field number which is 42.
🔧 Debug
advanced2:00remaining
Why does this Kotlin code fail to call Java method?
This Kotlin code tries to call a Java method but fails. What is the cause?
Kotlin
public class JavaExample { private String secret() { return "hidden"; } } fun main() { val example = JavaExample() println(example.secret()) }
Attempts:
2 left
💡 Hint
Kotlin respects Java access modifiers like private.
✗ Incorrect
The secret() method is private in Java, so Kotlin cannot call it, causing a compilation error.
📝 Syntax
advanced2:00remaining
Correct Kotlin syntax to call Java static method
Which Kotlin code correctly calls a Java static method?
Kotlin
public class JavaUtil { public static String staticMethod() { return "Called"; } } // Kotlin code options:
Attempts:
2 left
💡 Hint
Java static methods can be called directly on the class.
✗ Incorrect
Java static methods are callable directly in Kotlin using the class name.
🚀 Application
expert3:00remaining
How to handle Java nullability in Kotlin safely?
Given a Java method that may return null, which Kotlin code safely handles the null value?
Kotlin
public class JavaNull { public static String getNullable() { return null; } } // Kotlin code options:
Attempts:
2 left
💡 Hint
Use Kotlin nullable types and the Elvis operator to handle possible nulls.
✗ Incorrect
Option A safely assigns the nullable Java return to a nullable Kotlin variable and provides a default value if null.