Bird
Raised Fist0
Kotlinprogramming~20 mins

Calling Kotlin from Java - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. Which statement is true about calling Kotlin functions from Java?
easy
A. You can call Kotlin class functions like normal Java methods.
B. You cannot call Kotlin functions from Java directly.
C. Kotlin functions must be rewritten in Java to be used.
D. Kotlin functions are only accessible in Kotlin files.

Solution

  1. Step 1: Understand Kotlin-Java interoperability

    Kotlin classes compile to Java bytecode, so their functions appear as normal Java methods.
  2. Step 2: Confirm Java can call Kotlin class functions

    Java code can create Kotlin objects and call their methods just like Java methods.
  3. Final Answer:

    You can call Kotlin class functions like normal Java methods. -> Option A
  4. Quick Check:

    Kotlin class functions = callable from Java [OK]
Hint: Kotlin classes compile to Java classes, so methods are callable [OK]
Common Mistakes:
  • Thinking Kotlin functions need rewriting in Java
  • Believing Kotlin functions are inaccessible from Java
  • Confusing top-level functions with class functions
2. Which is the correct way to call a top-level Kotlin function fun greet() from Java?
easy
A. KotlinFileKt.greet();
B. greet();
C. new KotlinFile().greet();
D. Kotlin.greet();

Solution

  1. Step 1: Understand top-level function compilation

    Top-level Kotlin functions compile into static methods inside a generated class named after the Kotlin file with 'Kt' suffix.
  2. Step 2: Call the function from Java using generated class

    Java calls the function as a static method on the generated class, e.g., KotlinFileKt.greet().
  3. Final Answer:

    KotlinFileKt.greet(); -> Option A
  4. Quick Check:

    Top-level function call = KotlinFileKt.greet() [OK]
Hint: Top-level Kotlin functions appear in Java as static methods in FileNameKt class [OK]
Common Mistakes:
  • Trying to call top-level function directly without class
  • Assuming top-level functions need an instance
  • Using wrong class name without 'Kt' suffix
3. Given Kotlin file Utils.kt with top-level function:
fun add(a: Int, b: Int): Int = a + b

What is the output of this Java code?
System.out.println(UtilsKt.add(3, 4));
medium
A. Runtime exception
B. 34
C. Compile error
D. 7

Solution

  1. Step 1: Identify the Kotlin function behavior

    The Kotlin function adds two integers and returns the sum.
  2. Step 2: Understand Java call to Kotlin top-level function

    Java calls UtilsKt.add(3, 4), which returns 3 + 4 = 7.
  3. Final Answer:

    7 -> Option D
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Top-level Kotlin functions return expected results when called from Java [OK]
Common Mistakes:
  • Confusing concatenation with addition
  • Expecting compile error due to Kotlin-Java call
  • Assuming runtime exception from Kotlin code
4. What is wrong with this Java code calling Kotlin class Person with function fun greet()?:
Person p = new Person();
p.greet;
medium
A. greet is a private function in Kotlin.
B. Person class cannot be instantiated in Java.
C. Missing parentheses to call the function.
D. Kotlin functions cannot be called from Java.

Solution

  1. Step 1: Check Java method call syntax

    In Java, methods must be called with parentheses, even if no arguments.
  2. Step 2: Identify the error in code

    Code uses p.greet; missing parentheses, causing syntax error.
  3. Final Answer:

    Missing parentheses to call the function. -> Option C
  4. Quick Check:

    Method call syntax requires () [OK]
Hint: Always use parentheses to call methods in Java [OK]
Common Mistakes:
  • Omitting parentheses when calling methods
  • Assuming Kotlin classes can't be instantiated in Java
  • Thinking Kotlin functions are private by default
5. You have a Kotlin file MathUtils.kt with:
@JvmStatic
fun multiply(a: Int, b: Int): Int = a * b

How should you call multiply from Java to get 20 when multiplying 4 and 5?
hard
A. MathUtilsKt.multiply(4, 5);
B. MathUtils.Companion.multiply(4, 5);
C. new MathUtils().multiply(4, 5);
D. MathUtils.multiply(4, 5);

Solution

  1. Step 1: Understand @JvmStatic effect on Kotlin functions

    @JvmStatic makes the function a static method inside the companion object, callable via the Companion instance.
  2. Step 2: Identify correct Java call

    Java calls MathUtils.Companion.multiply(4, 5) to get 20.
  3. Final Answer:

    MathUtils.Companion.multiply(4, 5); -> Option B
  4. Quick Check:

    @JvmStatic function call = MathUtils.Companion.multiply() [OK]
Hint: @JvmStatic makes Kotlin function callable as static method in Java companion object [OK]
Common Mistakes:
  • Calling via generated Kt class ignoring @JvmStatic
  • Trying to instantiate Kotlin object unnecessarily
  • Calling via Companion object when not needed
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