Bird
0
0

Why does the Kotlin for loop for (i in 1..5) always include the number 5 in its iterations?

hard📝 Conceptual Q10 of 15
Kotlin - Loops and Ranges
Why does the Kotlin for loop for (i in 1..5) always include the number 5 in its iterations?
ABecause the range operator '..' is inclusive of the end value
BBecause Kotlin loops always run one extra iteration
CBecause the loop variable is incremented after the last iteration
DBecause the range excludes the start value
Step-by-Step Solution
Solution:
  1. Step 1: Understand the '..' operator behavior

    The '..' operator in Kotlin creates a closed range including both start and end values.
  2. Step 2: Apply to the loop

    The loop iterates from 1 up to and including 5 because 5 is part of the range.
  3. Final Answer:

    Because the range operator '..' is inclusive of the end value -> Option A
  4. Quick Check:

    Range operator '..' includes end = A [OK]
Quick Trick: .. operator includes both start and end numbers [OK]
Common Mistakes:
MISTAKES
  • Thinking '..' excludes the end value
  • Confusing loop increment with range behavior
  • Assuming loops run extra iterations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes