0
0
Kotlinprogramming~20 mins

Calling Kotlin from Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin-Java Interop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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"));
    }
}
AHello, Alice!
Bgreet
CHello, name!
DCompilation error due to Kotlin-Java interop
Attempts:
2 left
💡 Hint
Remember Kotlin functions are callable from Java with the same method names.
🧠 Conceptual
intermediate
2: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)
AJava accesses 'name' via getName() and 'age' via getAge() and setAge() methods.
BJava accesses 'name' via name() method and 'age' via age() method.
CJava cannot access Kotlin properties at all.
DJava accesses 'name' and 'age' directly as fields without methods.
Attempts:
2 left
💡 Hint
Kotlin properties generate getter and setter methods for Java.
🔧 Debug
advanced
2: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);
    }
}
AThe Kotlin function multiply must be inside a class to be called from Java.
BJava cannot call Kotlin top-level functions without @JvmName or using the generated class name.
CJava code must import kotlin.* to call Kotlin functions.
DThe Kotlin function multiply is private by default and not visible to Java.
Attempts:
2 left
💡 Hint
Kotlin top-level functions compile to static methods in a generated class.
📝 Syntax
advanced
2: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
    }
}
Aint sum = Calculator.Companion().add(5, 7);
Bint sum = Calculator.add(5, 7);
Cint sum = Calculator.add.Companion(5, 7);
Dint sum = Calculator.Companion.add(5, 7);
Attempts:
2 left
💡 Hint
Kotlin companion object functions are accessed via Companion instance in Java.
🚀 Application
expert
3: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)
        }
    }
}
AJava can call printMessage() without arguments only if the function is static.
BJava must always provide all arguments; default parameters are ignored.
CJava can call printMessage() with zero, one, or two arguments because @JvmOverloads generates overloads.
DJava cannot call Kotlin functions with default parameters at all.
Attempts:
2 left
💡 Hint
The @JvmOverloads annotation generates multiple Java methods with different parameters.