Bird
0
0

What does the following Kotlin code do?

easy📝 Conceptual Q11 of 15
Kotlin - Loops and Ranges
What does the following Kotlin code do?
for (i in 1..5) { println(i) }
APrints numbers from 1 to 4
BPrints numbers from 1 to 5 inclusive
CPrints numbers from 0 to 5
DPrints numbers from 5 down to 1
Step-by-Step Solution
Solution:
  1. Step 1: Understand the range operator

    The operator 1..5 creates a range including numbers 1, 2, 3, 4, and 5.
  2. Step 2: Loop through the range

    The for loop runs from 1 to 5, printing each number.
  3. Final Answer:

    Prints numbers from 1 to 5 inclusive -> Option B
  4. Quick Check:

    Range 1..5 includes 5 [OK]
Quick Trick: .. means inclusive range from start to end [OK]
Common Mistakes:
MISTAKES
  • Thinking .. excludes the last number
  • Confusing range with step or downTo
  • Assuming it counts down automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes