Bird
0
0

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

hard📝 Application Q8 of 15
Kotlin - Operators and Expressions
You want to print all even numbers from 2 to 20 using a range and step. Which Kotlin code snippet correctly does this?
Afor (i in 2..20 step 2) println(i)
Bfor (i in 2..20) if (i % 2 == 0) println(i)
Cfor (i in 2..20 step 1) if (i % 2 == 0) println(i)
Dfor (i in 2..20 step 3) println(i)
Step-by-Step Solution
Solution:
  1. Step 1: Understand step usage in ranges

    Using step 2 iterates over every second number starting at 2.
  2. Step 2: Check options

    for (i in 2..20 step 2) println(i) uses step 2 correctly to print even numbers directly.
  3. Final Answer:

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

    Use 'step' to skip numbers in range [OK]
Quick Trick: Use 'step' to jump numbers in range [OK]
Common Mistakes:
MISTAKES
  • Using wrong step value
  • Not using step and filtering instead
  • Using step 3 prints wrong numbers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes