Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a Java ArrayList in Kotlin.
Kotlin
val list = [1]<String>() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Kotlin's List which is immutable by default.
Using Set which is a different collection type.
✗ Incorrect
In Kotlin, to create a Java ArrayList, you use ArrayList which is interoperable with Java's ArrayList.
2fill in blank
mediumComplete the code to call a Java method from Kotlin.
Kotlin
val result = javaObject.[1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that don't exist on the Java object.
Trying to call Kotlin-specific functions on Java objects.
✗ Incorrect
The toString() method is a common Java method that can be called from Kotlin on any Java object.
3fill in blank
hardFix the error in calling a Java static method from Kotlin.
Kotlin
val max = Math.[1](5, 10)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Kotlin's maxOf instead of Java's Math.max.
Using incorrect method names that don't exist.
✗ Incorrect
The Java Math class has a static method called max to get the maximum of two numbers.
4fill in blank
hardFill both blanks to create a Java HashMap and add an entry in Kotlin.
Kotlin
val map = [1]<String, Int>() map.[2]("key", 1)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Kotlin's MutableMap instead of Java's HashMap.
Using add instead of put to add entries.
✗ Incorrect
Java's HashMap is used to create the map, and put adds a key-value pair.
5fill in blank
hardFill all three blanks to iterate over a Java ArrayList in Kotlin and print each item.
Kotlin
val list = [1]<String>() list.add("apple") for ([2] in list) { println([3]) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Kotlin's MutableList instead of Java's ArrayList.
Using different variable names inconsistently.
✗ Incorrect
We create a Java ArrayList, use item as the loop variable, and print item.