Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q14 of 15
Kotlin - Operators and Expressions
Identify the error in this Kotlin code snippet:
for (i in 1..5)
    if (i in 3..1) println(i)
AThe for loop syntax is incorrect
BThe range 3..1 is invalid because start is greater than end
CNo error, code runs fine
DThe if condition should use 'not in' instead of 'in'
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the range 3..1

    In Kotlin, a range with start greater than end (3..1) creates an empty range, which is valid.
  2. Step 2: Verify code execution

    The for loop runs for i=1 to 5, but i in 3..1 is always false for any i, so nothing prints. No syntax or runtime errors occur.
  3. Final Answer:

    No error, code runs fine -> Option C
  4. Quick Check:

    Empty ranges (start > end) are valid in Kotlin [OK]
Quick Trick: Ranges with start > end are empty but valid [OK]
Common Mistakes:
MISTAKES
  • Thinking reversed ranges like 3..1 are invalid
  • Assuming the for loop syntax is wrong
  • Expecting a syntax error or crash with empty range

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes