Bird
0
0

Find the error in this Kotlin code snippet:

medium📝 Debug Q14 of 15
Kotlin - Loops and Ranges
Find the error in this Kotlin code snippet:
for (i in 10..1 step 2) {
  println(i)
}
Aprintln cannot be used inside for loop
Bstep cannot be used with ranges
CVariable i is not declared
DRange 10..1 is invalid for counting down
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the range 10..1

    The range 10..1 tries to count up from 10 to 1, which is invalid because 10 is greater than 1.
  2. Step 2: Understand correct descending syntax

    To count down, use downTo instead of .., e.g., 10 downTo 1 step 2.
  3. Final Answer:

    Range 10..1 is invalid for counting down -> Option D
  4. Quick Check:

    Use downTo for descending ranges [OK]
Quick Trick: .. always counts up; use downTo to count down [OK]
Common Mistakes:
MISTAKES
  • Using .. for descending ranges
  • Thinking step fixes descending range
  • Ignoring Kotlin's range direction rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes