Bird
0
0

Which of the following is the correct syntax to loop over a list items with index in Kotlin?

easy📝 Syntax Q12 of 15
Kotlin - Loops and Ranges
Which of the following is the correct syntax to loop over a list items with index in Kotlin?
Afor ((index, value) in items.withIndex()) { /* code */ }
Bfor (index, value in items) { /* code */ }
Cfor (index; value in items.withIndex()) { /* code */ }
Dfor index, value in items.withIndex() { /* code */ }
Step-by-Step Solution
Solution:
  1. Step 1: Recall Kotlin destructuring syntax in loops

    Kotlin allows destructuring pairs using parentheses in the for loop header, like for ((index, value) in ...).
  2. Step 2: Match syntax with withIndex()

    The correct syntax uses items.withIndex() and destructures the pair into index and value.
  3. Final Answer:

    for ((index, value) in items.withIndex()) { /* code */ } -> Option A
  4. Quick Check:

    Destructure pairs with (index, value) [OK]
Quick Trick: Use parentheses to destructure pairs in for loop [OK]
Common Mistakes:
MISTAKES
  • Omitting parentheses around (index, value)
  • Using semicolon instead of comma
  • Missing 'in' keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes