Bird
0
0

Which of the following Kotlin for loop syntaxes correctly uses withIndex() to iterate over a list items?

easy📝 Syntax Q3 of 15
Kotlin - Loops and Ranges
Which of the following Kotlin for loop syntaxes correctly uses withIndex() to iterate over a list items?
Afor ((index, item) in items.withIndex()) { println("$index: $item") }
Bfor (index, item in items.withIndex()) { println("$index: $item") }
Cfor (index; item in items.withIndex()) { println("$index: $item") }
Dfor (index item in items.withIndex()) { println("$index: $item") }
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax for destructuring pairs

    Correct syntax uses parentheses and comma: for ((index, item) in items.withIndex()).
  2. Step 2: Validate loop body

    Printing "$index: $item" is valid and shows index and item.
  3. Final Answer:

    for ((index, item) in items.withIndex()) { println("$index: $item") } -> Option A
  4. Quick Check:

    Correct destructuring syntax = for ((index, item) in items.withIndex()) { println("$index: $item") } [OK]
Quick Trick: Use parentheses and comma to destructure pairs [OK]
Common Mistakes:
MISTAKES
  • Missing parentheses
  • Using semicolon instead of comma
  • Omitting comma between variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes