0
0
Kotlinprogramming~20 mins

For loop with index (withIndex) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
For Loop with Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of for loop with withIndex on a list
What is the output of this Kotlin code snippet?
val fruits = listOf("apple", "banana", "cherry")
for ((index, fruit) in fruits.withIndex()) {
    println("$index: $fruit")
}
Kotlin
val fruits = listOf("apple", "banana", "cherry")
for ((index, fruit) in fruits.withIndex()) {
    println("$index: $fruit")
}
A
apple: 0
banana: 1
cherry: 2
B
0: apple
1: banana
2: cherry
C
apple
banana
cherry
D
0: apple
1: banana
2: cherry
3: null
Attempts:
2 left
💡 Hint
Remember that withIndex() provides both the index and the element in the loop.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in for loop with withIndex
Which option contains a syntax error in using withIndex() in a for loop?
Kotlin
val numbers = listOf(10, 20, 30)
for ((i, num) in numbers.withIndex()) {
    println("Index $i has value $num")
}
Afor ((i, num) in numbers.withIndex()) { println(i + num) }
B} )mun + i(nltnirp { ))(xednIhtiw.srebmun ni )mun ,i(( rof
Cfor ((i, num) in numbers.withIndex()) println(i + num)
Dfor (i, num in numbers.withIndex()) { println(i + num) }
Attempts:
2 left
💡 Hint
Check how destructuring declarations are used in Kotlin for loops.
optimization
advanced
2:00remaining
Optimizing iteration with withIndex on large lists
Given a large list, which approach is more efficient to print index and value pairs?
Options:
A) Using withIndex() in a for loop
B) Using indices property and accessing elements by index
C) Using forEachIndexed function
D) Using a while loop with manual index increment
Afor ((index, value) in list.withIndex()) { println("$index: $value") }
Bfor (i in list.indices) { println("$i: ${list[i]}") }
Clist.forEachIndexed { index, value -> println("$index: $value") }
D
var i = 0
while (i < list.size) { println("$i: ${list[i]}"); i++ }
Attempts:
2 left
💡 Hint
Consider readability and internal optimizations of Kotlin standard library functions.
🔧 Debug
advanced
2:00remaining
Debug the unexpected output in for loop with withIndex
What causes this Kotlin code to print incorrect indices?
val items = listOf("a", "b", "c")
for ((index, item) in items.withIndex()) {
    if (item == "b") continue
    println("$index: $item")
}
AThe continue statement skips printing but does not skip index increment, so indices remain correct.
BThe loop should use break instead of continue to avoid index issues.
CThe index variable is not updated correctly because of continue, causing wrong indices.
DUsing continue inside for loop with withIndex causes index to reset to 0.
Attempts:
2 left
💡 Hint
Think about how continue affects loop iteration and index values.
🧠 Conceptual
expert
2:00remaining
Understanding withIndex behavior on different collections
Which statement about withIndex() in Kotlin is TRUE?
AwithIndex() can be used on any Iterable and returns an IndexedValue sequence with index starting at 0.
BwithIndex() modifies the original collection by adding index properties to elements.
CwithIndex() returns indices and values only for List collections, not for Sets.
DwithIndex() returns a Map of index to element for any collection.
Attempts:
2 left
💡 Hint
Recall what types of collections implement Iterable and how withIndex works.