Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Loops and Ranges
What will be the output of this Kotlin code?
var count = 3
while (count > 0) {
    println(count)
    count--
}
A3 2 1
B1 2 3
C3 2 1 0
D0 1 2 3
Step-by-Step Solution
Solution:
  1. Step 1: Analyze initial value and condition

    The variable count starts at 3. The loop runs while count > 0, so it runs for 3, 2, and 1.
  2. Step 2: Trace each iteration and output

    Each loop prints the current count then decreases it by 1. So it prints 3, then 2, then 1, then stops before printing 0.
  3. Final Answer:

    3 2 1 -> Option A
  4. Quick Check:

    Loop prints count down from 3 to 1 = 3 2 1 [OK]
Quick Trick: Count down loops print from start to 1, not zero [OK]
Common Mistakes:
MISTAKES
  • Including 0 in output
  • Printing in ascending order
  • Not decrementing count

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes