0
0
Kotlinprogramming~10 mins

Collections interop behavior in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AArrayList
BlistOf
CmutableListOf
DCollections
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.
2fill in blank
medium

Complete 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'
AtoCollection
BtoMutableList
CtoList
DtoJavaList
Attempts:
3 left
💡 Hint
Common Mistakes
Using toList() which returns an immutable Kotlin list, not a Java list.
Using toJavaList() which does not exist.
3fill in blank
hard

Fix 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'
AtoList
BtoSet
CasList
DtoMutableList
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.
4fill in blank
hard

Fill 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'
AHashMap
B"x"
CmapOf
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using mapOf() which creates an empty map here.
Using 10 as a key instead of "x".
5fill in blank
hard

Fill 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'
AArrayList
B>
C1
DlistOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using listOf which creates an immutable list.
Using '<' instead of '>' in the filter.