0
0
Kotlinprogramming~20 mins

Why Java interop matters in Kotlin - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Java Interop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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"))
}
AError: Unresolved reference to JavaClass
BHello, Kotlin from Kotlin!
CHello, Kotlin from Java!
DHello from Java!
Attempts:
2 left
💡 Hint
Remember Kotlin can call Java methods directly if the Java class is accessible.
🧠 Conceptual
intermediate
1:30remaining
Why use Java interop in Kotlin?
Which of the following is the main reason Kotlin supports Java interoperability?
ATo force Kotlin developers to write Java code
BTo prevent Kotlin from compiling to JVM bytecode
CTo make Kotlin incompatible with Android development
DTo reuse existing Java libraries and frameworks seamlessly
Attempts:
2 left
💡 Hint
Think about how Kotlin benefits from the large Java ecosystem.
🔧 Debug
advanced
2: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)
}
Aprintln(name.length)
Bprintln(name?.length ?: "No name")
Cprintln(name!!.length)
Dprintln(name.length ?: 0)
Attempts:
2 left
💡 Hint
Use Kotlin's safe call operator and Elvis operator to handle nulls.
📝 Syntax
advanced
1: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;
    }
}
Aval result = JavaUtils.add(3, 4)
Bval result = JavaUtils.add(3)(4)
Cval result = JavaUtils.add.call(3, 4)
Dval result = JavaUtils().add(3, 4)
Attempts:
2 left
💡 Hint
Static methods in Java are called on the class, not an instance.
🚀 Application
expert
3: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])
}
A
2
cherry
B
3
banana
C
2
banana
D
3
cherry
Attempts:
2 left
💡 Hint
Removing "banana" reduces size and shifts elements.