Bird
0
0

What is the output of this Kotlin code?

medium📝 Predict Output Q4 of 15
Kotlin - Loops and Ranges
What is the output of this Kotlin code?
for (i in 10 downTo 2 step 4) {
    print(i)
}
A10 6 2
B10 8 6 4 2
C10 6 4 2
D10 6 2 0
Step-by-Step Solution
Solution:
  1. Step 1: Identify loop values with downTo and step

    Start at 10, count down to 2, stepping by 4 means subtract 4 each time: 10, 6, 2.
  2. Step 2: Confirm loop stops at or above 2

    Next step after 2 would be -2 which is below 2, so loop ends.
  3. Final Answer:

    10 6 2 -> Option A
  4. Quick Check:

    downTo with step 4 outputs 10 6 2 [OK]
Quick Trick: Count down subtracting step until reaching end value [OK]
Common Mistakes:
MISTAKES
  • Including numbers below end value
  • Confusing step direction
  • Printing spaces instead of concatenated output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes