Bird
0
0

Which of the following is the correct syntax for a Kotlin for loop that counts from 2 to 10 stepping by 2?

easy📝 Syntax Q3 of 15
Kotlin - Loops and Ranges
Which of the following is the correct syntax for a Kotlin for loop that counts from 2 to 10 stepping by 2?
Afor (i in 2..10 downTo 2) { println(i) }
Bfor (i in 2..10 step 2) { println(i) }
Cfor (i in 10 downTo 2 step 2) { println(i) }
Dfor (i in 2..10 step) { println(i) }
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct range and step syntax

    Counting from 2 to 10 uses 2..10 and stepping by 2 uses step 2.
  2. Step 2: Check syntax correctness

    for (i in 2..10 step 2) { println(i) } uses correct syntax: for (i in 2..10 step 2). Others misuse downTo or omit step value.
  3. Final Answer:

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

    Range with step syntax = correct loop [OK]
Quick Trick: Use '..' for ascending, 'downTo' for descending loops [OK]
Common Mistakes:
MISTAKES
  • Using downTo with ascending range
  • Omitting step value
  • Wrong step placement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes