You call a Kotlin function from Java like a regular Java method. Kotlin compiles to JVM bytecode, so Java can use Kotlin classes and functions directly.
Click to reveal answer
beginner
What is the default Kotlin class name seen from Java if no @JvmName is used?
If you have top-level Kotlin functions in a file named Example.kt, Java sees them in a class named ExampleKt by default.
Click to reveal answer
intermediate
What annotation helps rename Kotlin functions or classes for Java callers?
The @JvmName annotation lets you specify a different name for Kotlin functions or classes when called from Java.
Click to reveal answer
intermediate
How do Kotlin properties appear when accessed from Java?
Kotlin properties appear as getter and setter methods in Java. For example, a Kotlin property 'val name' becomes 'getName()' in Java.
Click to reveal answer
intermediate
What is the purpose of the @JvmStatic annotation in Kotlin for Java callers?
@JvmStatic makes a Kotlin function or property static in the generated Java bytecode, so Java can call it without an instance.
Click to reveal answer
If you have a Kotlin file named Utils.kt with a top-level function, how do you call it from Java?
AUtilsKt.functionName()
BUtils.functionName()
Cnew Utils().functionName()
DYou cannot call it from Java
✗ Incorrect
Top-level Kotlin functions are placed in a class named after the file with 'Kt' appended, so Java calls UtilsKt.functionName().
Which annotation changes the name of a Kotlin function for Java callers?
A@JvmStatic
B@JvmName
C@JvmOverloads
D@JvmField
✗ Incorrect
@JvmName lets you rename Kotlin functions or classes for Java interoperability.
How does a Kotlin 'val' property appear in Java?
AAs a public field
BAs a setter method
CAs a private field with no access
DAs a getter method
✗ Incorrect
A Kotlin 'val' property appears as a getter method in Java, e.g., getPropertyName().
What does the @JvmStatic annotation do in Kotlin?
AMakes a function static in Java bytecode
BGenerates overloads
CChanges function visibility
DMakes a function inline
✗ Incorrect
@JvmStatic makes the annotated function or property static in the generated Java bytecode.
Can Java call Kotlin extension functions directly?
AYes, as instance methods
BNo, extension functions are invisible to Java
CYes, as static methods
DOnly if annotated with @JvmName
✗ Incorrect
Kotlin extension functions become static methods in Java and can be called like static methods.
Explain how Kotlin top-level functions are accessed from Java and how you can customize their names.
Think about the file name and the default class name Java sees.
You got /3 concepts.
Describe how Kotlin properties are represented in Java and how @JvmStatic affects calling Kotlin functions from Java.
Consider how Java accesses fields and static methods.
You got /3 concepts.
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
Step 1: Understand Kotlin-Java interoperability
Kotlin classes compile to Java bytecode, so their functions appear as normal Java methods.
Step 2: Confirm Java can call Kotlin class functions
Java code can create Kotlin objects and call their methods just like Java methods.
Final Answer:
You can call Kotlin class functions like normal Java methods. -> Option A
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
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.
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().
Final Answer:
KotlinFileKt.greet(); -> Option A
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
Step 1: Identify the Kotlin function behavior
The Kotlin function adds two integers and returns the sum.
Step 2: Understand Java call to Kotlin top-level function