Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an instance of the Java class JavaClass in Kotlin.
Kotlin
val javaObj = [1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or incorrect class names.
Trying to use Kotlin-specific syntax for Java class instantiation.
✗ Incorrect
In Kotlin, you create an instance of a Java class by calling its constructor just like in Java, using the class name.
2fill in blank
mediumComplete the code to call the Java method getGreeting() from the Kotlin object javaObj.
Kotlin
val message = javaObj.[1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Changing the case of the method name.
Using underscores instead of camelCase.
✗ Incorrect
Java methods are called in Kotlin using the exact method name with camelCase, matching Java naming conventions.
3fill in blank
hardFix the error in calling the Java static method JavaClass.staticMethod() from Kotlin.
Kotlin
val result = JavaClass.[1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Changing method name casing.
Trying to call static methods like instance methods.
✗ Incorrect
Static methods in Java are called in Kotlin using the exact method name with correct casing.
4fill in blank
hardFill both blanks to create a Kotlin function that calls a Java method calculateSum(int a, int b) and returns the result.
Kotlin
fun sum(a: Int, b: Int): Int = javaObj.[1]([2], b)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names.
Passing wrong variables as arguments.
✗ Incorrect
Call the Java method by its exact name and pass the Kotlin function parameter
a as the first argument.5fill in blank
hardFill all three blanks to create a Kotlin property that calls a Java getter method getName() from javaObj.
Kotlin
val name: String
get() = javaObj.[1]()
fun printName() {
println([2])
println(javaObj.[3]())
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect casing for method names.
Printing the method call instead of the property.
✗ Incorrect
Use the Java getter method
getName() exactly for the property getter and print statements. The property name is name.