Bird
0
0

You want to print numbers from 10 down to 1 using a loop that runs at least once. Which Kotlin code is correct?

hard📝 Application Q8 of 15
Kotlin - Loops and Ranges
You want to print numbers from 10 down to 1 using a loop that runs at least once. Which Kotlin code is correct?
Avar i = 10 do { print(i) i-- } while (i > 0)
Bvar i = 10 while (i > 10) { print(i) i-- }
Cvar i = 10 while i > 0 { print(i) i-- }
Dvar i = 10 do { print(i) i-- } while i > 0
Step-by-Step Solution
Solution:
  1. Step 1: Identify loop type that runs at least once

    Only do-while guarantees the body runs once.
  2. Step 2: Check syntax correctness

    var i = 10 do { print(i) i-- } while (i > 0) uses correct do-while syntax with parentheses and decrements i.
  3. Final Answer:

    var i = 10 do { print(i) i-- } while (i > 0) -> Option A
  4. Quick Check:

    do-while with parentheses and decrement = A [OK]
Quick Trick: Use do-while with parentheses for guaranteed one run [OK]
Common Mistakes:
MISTAKES
  • Missing parentheses
  • Using while without braces
  • Wrong decrement placement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes