0
0
Kotlinprogramming~20 mins

Calling Java from Kotlin seamlessly - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Java-Kotlin Interop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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"))
}
ARuntime error: NullPointerException
BHello, null!
CCompilation error: Cannot find symbol greet
DHello, Alice!
Attempts:
2 left
💡 Hint
Java static methods can be called directly from Kotlin using the class name.
Predict Output
intermediate
2: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)
}
A42
B0
CCompilation error: Cannot access field number
DRuntime error: IllegalAccessException
Attempts:
2 left
💡 Hint
Public Java fields are accessible directly from Kotlin.
🔧 Debug
advanced
2: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())
}
AOutput: hidden
BRuntime error: NoSuchMethodException
CCompilation error: secret() has private access in JavaExample
DCompilation error: Cannot find symbol secret
Attempts:
2 left
💡 Hint
Kotlin respects Java access modifiers like private.
📝 Syntax
advanced
2: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:
Aprintln(JavaUtil.staticMethod)
Bprintln(JavaUtil.staticMethod())
Cprintln(JavaUtil().staticMethod())
Dprintln(JavaUtil.Companion.staticMethod())
Attempts:
2 left
💡 Hint
Java static methods can be called directly on the class.
🚀 Application
expert
3: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:
A
val result: String? = JavaNull.getNullable()
println(result ?: "default")
B
val result: String = JavaNull.getNullable()!!
println(result)
C
val result: String = JavaNull.getNullable()
println(result)
D
val result = JavaNull.getNullable() ?: throw Exception("Null")
println(result)
Attempts:
2 left
💡 Hint
Use Kotlin nullable types and the Elvis operator to handle possible nulls.