Bird
0
0

You want to print all even numbers from 20 down to 2 using a Kotlin for loop. Which code snippet achieves this correctly?

hard📝 Application Q8 of 15
Kotlin - Loops and Ranges
You want to print all even numbers from 20 down to 2 using a Kotlin for loop. Which code snippet achieves this correctly?
Afor (i in 20 downTo 2 step 1) { if (i % 2 == 0) print(i) }
Bfor (i in 2..20 step 2) { print(i) }
Cfor (i in 20..2 step 2) { print(i) }
Dfor (i in 20 downTo 2 step 2) { print(i) }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct descending range and step

    Counting down from 20 to 2 by 2 uses 20 downTo 2 step 2.
  2. Step 2: Confirm output matches even numbers descending

    This loop prints 20, 18, 16, ..., 2, all even numbers descending.
  3. Final Answer:

    for (i in 20 downTo 2 step 2) { print(i) } -> Option D
  4. Quick Check:

    downTo with step 2 prints even numbers descending [OK]
Quick Trick: Use downTo with step 2 for descending even numbers [OK]
Common Mistakes:
MISTAKES
  • Using ascending range for descending numbers
  • Wrong step value
  • Filtering inside loop instead of stepping

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes