Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a Kotlin list from a Java list.
Kotlin
val javaList = java.util.ArrayList<String>() javaList.add("apple") val kotlinList: List<String> = [1](javaList)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using listOf() which creates an immutable list and does not accept a Java list directly.
Using mutableListOf() which creates an empty list.
✗ Incorrect
Use ArrayList(javaList) to create a Kotlin list from a Java list.
2fill in blank
mediumComplete the code to convert a Kotlin list to a Java list.
Kotlin
val kotlinList = listOf("one", "two", "three") val javaList: java.util.List<String> = kotlinList.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using toList() which returns an immutable Kotlin list, not a Java list.
Using toJavaList() which does not exist.
✗ Incorrect
Use toMutableList() to get a Java mutable list from a Kotlin list.
3fill in blank
hardFix the error in the code to add an element to a Java list obtained from a Kotlin list.
Kotlin
val kotlinList = listOf("a", "b") val javaList: java.util.List<String> = kotlinList.[1]() javaList.add("c")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using toList() which returns an immutable list causing add() to fail.
Using asList() which returns a fixed-size list.
✗ Incorrect
Use toMutableList() to get a mutable Java list that supports adding elements.
4fill in blank
hardFill both blanks to create a Kotlin map from a Java map and access a value.
Kotlin
val javaMap = java.util.HashMap<String, Int>() javaMap["x"] = 10 val kotlinMap: Map<String, Int> = [1](javaMap) val value = kotlinMap[[2]]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using mapOf() which creates an empty map here.
Using 10 as a key instead of "x".
✗ Incorrect
Use HashMap(javaMap) to create a Kotlin map from a Java map, and access the value with the key "x".
5fill in blank
hardFill all three blanks to filter a Kotlin list converted from a Java list.
Kotlin
val javaList = java.util.ArrayList<Int>() javaList.add(1) javaList.add(2) javaList.add(3) val kotlinList = [1](javaList) val filtered = kotlinList.filter { it [2] [3] }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using listOf which creates an immutable list.
Using '<' instead of '>' in the filter.
✗ Incorrect
Convert the Java list to a Kotlin ArrayList, then filter elements greater than 1.