Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q14 of 15
Kotlin - Loops and Ranges
Identify the error in this Kotlin code snippet:
val fruits = listOf("apple", "banana", "cherry")
for fruit in fruits {
    println(fruit)
}
AMissing parentheses around loop header
BUsing wrong collection type
CIncorrect print statement
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check for correct for loop syntax

    Kotlin requires parentheses around the loop header: for (fruit in fruits) { ... }
  2. Step 2: Identify the missing parentheses

    The code misses parentheses around 'fruit in fruits', causing a syntax error.
  3. Final Answer:

    Missing parentheses around loop header -> Option A
  4. Quick Check:

    Parentheses required in Kotlin for loops [OK]
Quick Trick: Always put parentheses around for loop header [OK]
Common Mistakes:
MISTAKES
  • Omitting parentheses in for loop
  • Using wrong loop variable syntax
  • Assuming print statement causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes