Challenge - 5 Problems
Java Interop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Kotlin calling Java method
What is the output of this Kotlin code that calls a Java method?
Kotlin
class JavaClass { fun greet(name: String): String { return "Hello, $name from Java!" } } fun main() { val javaObj = JavaClass() println(javaObj.greet("Kotlin")) }
Attempts:
2 left
💡 Hint
Remember Kotlin can call Java methods directly if the Java class is accessible.
✗ Incorrect
The Kotlin code creates an instance of the JavaClass and calls its greet method, passing "Kotlin". The Java method returns "Hello, Kotlin from Java!" which is printed.
🧠 Conceptual
intermediate1:30remaining
Why use Java interop in Kotlin?
Which of the following is the main reason Kotlin supports Java interoperability?
Attempts:
2 left
💡 Hint
Think about how Kotlin benefits from the large Java ecosystem.
✗ Incorrect
Kotlin interoperates with Java so developers can use the vast amount of existing Java libraries and frameworks without rewriting them.
🔧 Debug
advanced2:30remaining
Fix the Kotlin code calling a Java method with null safety
This Kotlin code calls a Java method that can return null. Which option correctly handles the null safely?
Kotlin
class JavaClass { fun getName(): String? { return null } } fun main() { val javaObj = JavaClass() val name = javaObj.getName() println(name.length) }
Attempts:
2 left
💡 Hint
Use Kotlin's safe call operator and Elvis operator to handle nulls.
✗ Incorrect
Since getName() can return null, using name?.length safely accesses length or returns null, and ?: provides a default string if null.
📝 Syntax
advanced1:30remaining
Correct Kotlin syntax to call Java static method
Given a Java class with a static method, which Kotlin code correctly calls it?
Kotlin
public class JavaUtils { public static int add(int a, int b) { return a + b; } }
Attempts:
2 left
💡 Hint
Static methods in Java are called on the class, not an instance.
✗ Incorrect
In Kotlin, Java static methods are called on the class name directly, like JavaUtils.add(3, 4).
🚀 Application
expert3:00remaining
Determine the output of Kotlin code using Java collections
What is the output of this Kotlin code that uses a Java ArrayList?
Kotlin
import java.util.ArrayList fun main() { val list = ArrayList<String>() list.add("apple") list.add("banana") list.add("cherry") list.remove("banana") println(list.size) println(list[1]) }
Attempts:
2 left
💡 Hint
Removing "banana" reduces size and shifts elements.
✗ Incorrect
After removing "banana", the list has "apple" and "cherry". Size is 2, and index 1 is "cherry".