Bird
0
0

You want to print all odd numbers from 1 to 15 using a Kotlin for loop. Which code snippet achieves this?

hard📝 Application Q8 of 15
Kotlin - Loops and Ranges
You want to print all odd numbers from 1 to 15 using a Kotlin for loop. Which code snippet achieves this?
Afor (i in 1..15) { if (i % 2 == 1) println(i) }
BAll of the above
Cfor (i in 1..15 step 1) { if (i % 2 != 0) println(i) }
Dfor (i in 1..15 step 2) { println(i) }
Step-by-Step Solution
Solution:
  1. Step 1: Analyze each option for printing odd numbers

    for (i in 1..15 step 2) { println(i) } uses step 2 starting at 1, so prints 1,3,5,...15. for (i in 1..15) { if (i % 2 == 1) println(i) } checks odd numbers with condition inside loop. for (i in 1..15 step 1) { if (i % 2 != 0) println(i) } does the same with step 1.
  2. Step 2: Confirm all options print odd numbers correctly

    All options print odd numbers from 1 to 15 correctly.
  3. Final Answer:

    All of the above -> Option B
  4. Quick Check:

    Multiple ways to print odds = A [OK]
Quick Trick: Use step 2 or condition to print odd numbers [OK]
Common Mistakes:
MISTAKES
  • Using step 2 starting at even number
  • Forgetting to check odd condition
  • Assuming only one correct method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes