Challenge - 5 Problems
Kotlin-Java Interop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of calling Kotlin function from Java
Consider a Kotlin class with a function called from Java. What will be the output when the Java code runs?
Kotlin
/* Kotlin file: Greeting.kt */ package example class Greeting { fun greet(name: String): String { return "Hello, $name!" } } /* Java file: Main.java */ package example; public class Main { public static void main(String[] args) { Greeting greeting = new Greeting(); System.out.println(greeting.greet("Alice")); } }
Attempts:
2 left
💡 Hint
Remember Kotlin functions are callable from Java with the same method names.
✗ Incorrect
The Kotlin class Greeting has a method greet that returns a greeting string. Java calls this method normally, so the output is the greeting with the passed name.
🧠 Conceptual
intermediate2:00remaining
Kotlin property access from Java
Given a Kotlin class with a property, how does Java access it?
Kotlin
/* Kotlin file: Person.kt */ package example class Person(val name: String, var age: Int)
Attempts:
2 left
💡 Hint
Kotlin properties generate getter and setter methods for Java.
✗ Incorrect
Kotlin properties compile to private fields with public getter and setter methods. Java uses these methods to access properties.
🔧 Debug
advanced2:00remaining
Why does this Java code fail to call Kotlin top-level function?
You have a Kotlin file with a top-level function. Java code tries to call it but fails. Identify the cause.
Kotlin
/* Kotlin file: Utils.kt */ package example fun multiply(a: Int, b: Int): Int = a * b /* Java file: Main.java */ package example; public class Main { public static void main(String[] args) { int result = Utils.multiply(3, 4); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Kotlin top-level functions compile to static methods in a generated class.
✗ Incorrect
Kotlin top-level functions are compiled into a class named after the file with 'Kt' suffix by default. Java must call example.UtilsKt.multiply(...) or use @file:JvmName annotation to rename the class.
📝 Syntax
advanced2:00remaining
Correct Java syntax to call Kotlin companion object function
Given a Kotlin class with a companion object function, which Java code correctly calls it?
Kotlin
/* Kotlin file: Calculator.kt */ package example class Calculator { companion object { fun add(a: Int, b: Int): Int = a + b } }
Attempts:
2 left
💡 Hint
Kotlin companion object functions are accessed via Companion instance in Java.
✗ Incorrect
In Java, companion object members are accessed through the static field 'Companion' of the class.
🚀 Application
expert3:00remaining
How to expose Kotlin function with default parameters to Java
Kotlin functions can have default parameter values. How can you call such a function from Java with default values?
Kotlin
/* Kotlin file: Printer.kt */ package example class Printer { @JvmOverloads fun printMessage(message: String = "Hello", times: Int = 1) { repeat(times) { println(message) } } }
Attempts:
2 left
💡 Hint
The @JvmOverloads annotation generates multiple Java methods with different parameters.
✗ Incorrect
Kotlin's @JvmOverloads creates overloaded methods for Java, allowing calls with fewer arguments using defaults.