Bird
Raised Fist0
Kotlinprogramming~20 mins

Calling Java from Kotlin seamlessly - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main advantage of calling Java code from Kotlin seamlessly?
easy
A. You can reuse existing Java code without extra wrappers or adapters.
B. You must convert Java code to Kotlin before using it.
C. Kotlin cannot call Java code directly; it requires JNI.
D. Java code runs slower when called from Kotlin.

Solution

  1. Step 1: Understand interoperability

    Kotlin is designed to work directly with Java code without needing extra conversion or wrappers.
  2. Step 2: Identify the benefit

    This direct use allows developers to reuse Java libraries and code easily in Kotlin projects.
  3. Final Answer:

    You can reuse existing Java code without extra wrappers or adapters. -> Option A
  4. Quick Check:

    Seamless interoperability = reuse Java code easily [OK]
Hint: Kotlin calls Java code directly without conversion [OK]
Common Mistakes:
  • Thinking Java code must be rewritten in Kotlin
  • Believing JNI is needed for Kotlin-Java calls
  • Assuming performance drops when calling Java from Kotlin
2. Which Kotlin syntax correctly creates an instance of a Java class named JavaHelper?
easy
A. val helper = JavaHelper.new()
B. val helper = new JavaHelper()
C. val helper = JavaHelper()
D. val helper = JavaHelper.create()

Solution

  1. Step 1: Recall Kotlin object creation syntax

    Kotlin creates objects by calling the constructor directly without the new keyword.
  2. Step 2: Apply to Java class

    Since Kotlin interoperates with Java, you create Java objects the same way: JavaHelper().
  3. Final Answer:

    val helper = JavaHelper() -> Option C
  4. Quick Check:

    Kotlin object creation = ClassName() [OK]
Hint: Use ClassName() without 'new' in Kotlin [OK]
Common Mistakes:
  • Using 'new' keyword like in Java
  • Trying to call static factory methods incorrectly
  • Confusing Kotlin syntax with Java syntax
3. Given this Java class:
public class Calculator {
    public int add(int a, int b) { return a + b; }
}

What will this Kotlin code print?
val calc = Calculator()
println(calc.add(3, 4))
medium
A. 7
B. 34
C. Compilation error
D. Runtime exception

Solution

  1. Step 1: Understand Java method call from Kotlin

    Kotlin can call Java methods directly. The add method sums two integers.
  2. Step 2: Calculate the output

    Calling calc.add(3, 4) returns 3 + 4 = 7, which is printed.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Java methods called like Kotlin methods return expected results [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Expecting syntax errors due to Java-Kotlin call
  • Confusing method call with property access
4. This Kotlin code tries to call a Java method but causes an error:
val helper = JavaHelper()
helper.doSomething()

What is the likely cause if doSomething() is not recognized?
medium
A. Kotlin requires 'new' keyword to call Java methods.
B. Kotlin cannot call Java methods with no parameters.
C. You must import the Java class explicitly in Kotlin.
D. The Java method is private or missing in the class.

Solution

  1. Step 1: Check method visibility and existence

    If Kotlin cannot find doSomething(), it may be private or not declared in the Java class.
  2. Step 2: Understand Kotlin-Java interoperability rules

    Kotlin can call any public Java method directly, so missing or private methods cause errors.
  3. Final Answer:

    The Java method is private or missing in the class. -> Option D
  4. Quick Check:

    Method visibility matters for Kotlin-Java calls [OK]
Hint: Check Java method is public and exists before calling [OK]
Common Mistakes:
  • Assuming Kotlin needs 'new' keyword
  • Forgetting to import Java class (usually auto-imported)
  • Thinking Kotlin can't call no-parameter methods
5. You have a Java class:
public class DataHolder {
    private String data;
    public DataHolder(String data) { this.data = data; }
    public String getData() { return data; }
    public void setData(String data) { this.data = data; }
}

How do you update and print the data from Kotlin?
hard
A. val holder = DataHolder("Hello") holder.data = "World" println(holder.data)
B. val holder = DataHolder("Hello") holder.setData("World") println(holder.getData())
C. val holder = DataHolder("Hello") holder.data.set("World") println(holder.data.get())
D. val holder = DataHolder("Hello") holder.setData = "World" println(holder.getData)

Solution

  1. Step 1: Understand Java private field access

    The data field is private, so Kotlin cannot access it directly.
  2. Step 2: Use Java getter and setter methods

    Kotlin calls setData() to update and getData() to read the value.
  3. Final Answer:

    val holder = DataHolder("Hello") holder.setData("World") println(holder.getData()) -> Option B
  4. Quick Check:

    Use Java getters/setters to access private fields [OK]
Hint: Use Java getters/setters for private fields in Kotlin [OK]
Common Mistakes:
  • Trying to access private fields directly
  • Using property syntax on Java methods incorrectly
  • Confusing method calls with property assignments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard