Bird
0
0

You want to print all odd numbers from 15 down to 1 using a Kotlin for loop with downTo and step, without using an if statement. Which code snippet correctly does this?

hard📝 Application Q15 of 15
Kotlin - Loops and Ranges
You want to print all odd numbers from 15 down to 1 using a Kotlin for loop with downTo and step, without using an if statement. Which code snippet correctly does this?
Afor (i in 15 downTo 1 step 2) { println(i) }
Bfor (i in 15 downTo 1 step 1) { if (i % 2 == 1) println(i) }
Cfor (i in 15 downTo 1 step 3) { println(i) }
Dfor (i in 14 downTo 1 step 2) { println(i) }
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    We want to print odd numbers from 15 down to 1. Starting at 15 (which is odd) and stepping by 2 will print all odd numbers downwards.
  2. Step 2: Evaluate options

    for (i in 15 downTo 1 step 2) { println(i) } starts at 15 and steps by 2 down to 1, printing 15, 13, 11, ..., 1. for (i in 15 downTo 1 step 1) { if (i % 2 == 1) println(i) } uses step 1 with an if filter (disallowed). for (i in 15 downTo 1 step 3) { println(i) } steps by 3, skipping some odds. for (i in 14 downTo 1 step 2) { println(i) } starts at 14 (even), printing evens.
  3. Final Answer:

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

    Start odd, step 2 downTo 1 = odd numbers [OK]
Quick Trick: Start at odd number, step 2 downTo 1 for odd numbers [OK]
Common Mistakes:
MISTAKES
  • Starting at even number when printing odds
  • Using wrong step size
  • Filtering inside loop instead of stepping

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes