Open
Description
Q1. You would like to print each score on its own line with its cardinal position. Without using var or val, which method allows iteration with both the value and its position?
fun main() {
val highScores = listOf(4000, 2000, 10200, 12000, 9030)
}
Actual Answer
- .withIndex()
- .forEachIndexed()
- .forEach()
- .forIndexes()
Expected Answer
- .withIndex()
- .forEachIndexed()
- .forEach()
- .forIndexes()
Explanation:
withIndex()
returns an Sequence<IndexedValue<T>>
which still holds a Sequence but does not iterate through it. The correct answer should be .forEachIndexed()
which does allow iteration through the Sequence with both the value and its position.