Bird
0
0

You want to print all even numbers from 2 to 10 using a range. Which Kotlin code correctly does this?

hard📝 Application Q8 of 15
Kotlin - Loops and Ranges
You want to print all even numbers from 2 to 10 using a range. Which Kotlin code correctly does this?
Afor (i in 2..10 step 2) { println(i) }
Bfor (i in 2..10 step 3) { println(i) }
Cfor (i in 1..10 step 2) { println(i) }
Dfor (i in 2 until 10) { if (i % 2 == 0) println(i) }
Step-by-Step Solution
Solution:
  1. Step 1: Identify even numbers from 2 to 10

    Even numbers are 2, 4, 6, 8, 10.
  2. Step 2: Use step 2 to iterate over evens

    Starting at 2 and stepping by 2 prints all even numbers up to 10.
  3. Final Answer:

    for (i in 2..10 step 2) { println(i) } -> Option A
  4. Quick Check:

    Step 2 from 2 prints evens [OK]
Quick Trick: Use step 2 to iterate over even numbers [OK]
Common Mistakes:
MISTAKES
  • Using wrong step value
  • Starting from 1 instead of 2
  • Using until excludes 10

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes