Bird
Raised Fist0
Kotlinprogramming~5 mins

Range operator (..) and in operator in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the range operator .. do in Kotlin?
The range operator .. creates a range of values from a start to an end, including both ends. For example, 1..5 means numbers 1, 2, 3, 4, and 5.
Click to reveal answer
beginner
How do you check if a value is inside a range in Kotlin?
You use the in operator. For example, 3 in 1..5 checks if 3 is between 1 and 5 (inclusive). It returns true if yes, false if no.
Click to reveal answer
beginner
What will this code print?
for (i in 1..3) {
    println(i)
}
It will print:
1
2
3
Because the loop goes through the range from 1 to 3, including both ends.
Click to reveal answer
intermediate
Can the in operator be used with characters in Kotlin?
Yes! You can check if a character is inside a range of characters. For example, 'b' in 'a'..'d' returns true because 'b' is between 'a' and 'd'.
Click to reveal answer
intermediate
What does 5 !in 1..4 mean in Kotlin?
It means 5 is NOT in the range from 1 to 4. The !in operator checks if a value is outside the range. Here, it returns true because 5 is not between 1 and 4.
Click to reveal answer
What does 1..5 represent in Kotlin?
AA list of numbers from 1 to 4
BA range including numbers 1 to 5
CA range excluding 5
DA function call
What is the result of 3 in 1..5?
Afalse
BError
Ctrue
Dnull
Which operator checks if a value is NOT in a range?
Ain
B..
C!=
D!in
What will this print?
for (c in 'a'..'c') println(c)
Aa b c
Ba b
Cb c
Da c
Is 10 in 1..9 true or false?
Afalse
Btrue
CError
DDepends on context
Explain how the range operator .. works in Kotlin and give an example.
Think about counting numbers from one to another.
You got /3 concepts.
    Describe how to use the in operator to check if a value is inside a range.
    It's like asking if something is inside a box.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does the Kotlin expression 1..5 represent?
      easy
      A. An error because ranges need parentheses
      B. A list containing numbers 1 and 5 only
      C. A function that returns 1 or 5
      D. A range of numbers from 1 to 5 inclusive

      Solution

      1. Step 1: Understand the range operator

        The .. operator creates a range including both start and end values.
      2. Step 2: Interpret 1..5

        This means all numbers from 1 up to 5, including 1 and 5.
      3. Final Answer:

        A range of numbers from 1 to 5 inclusive -> Option D
      4. Quick Check:

        1..5 = numbers 1 to 5 [OK]
      Hint: Remember: .. includes both ends in Kotlin ranges [OK]
      Common Mistakes:
      • Thinking it creates a list instead of a range
      • Assuming it excludes the last number
      • Confusing it with a function call
      2. Which of the following is the correct syntax to check if variable x is between 10 and 20 inclusive in Kotlin?
      easy
      A. if (x between 10 and 20) { }
      B. if (x in 10..20) { }
      C. if (10 <= x <= 20) { }
      D. if (x inside 10..20) { }

      Solution

      1. Step 1: Recall Kotlin's in operator syntax

        Kotlin uses in to check if a value is inside a range.
      2. Step 2: Match syntax with options

        Only if (x in 10..20) correctly uses in and the range operator ...
      3. Final Answer:

        if (x in 10..20) { } -> Option B
      4. Quick Check:

        Use in with .. for range checks [OK]
      Hint: Use 'in' with '..' for range checks in Kotlin [OK]
      Common Mistakes:
      • Using 'between' or 'inside' which are not Kotlin keywords
      • Trying chained comparisons like 10 <= x <= 20
      • Missing the range operator '..'
      3. What is the output of this Kotlin code?
      val x = 7
      if (x in 1..5) println("Inside") else println("Outside")
      medium
      A. Outside
      B. Compilation error
      C. Inside
      D. No output

      Solution

      1. Step 1: Understand the range check

        The range 1..5 includes numbers 1, 2, 3, 4, 5.
      2. Step 2: Check if x=7 is in the range

        7 is not between 1 and 5, so the condition is false.
      3. Final Answer:

        Outside -> Option A
      4. Quick Check:

        7 not in 1..5 means print "Outside" [OK]
      Hint: Check if value lies within range boundaries [OK]
      Common Mistakes:
      • Assuming 7 is inside 1..5
      • Confusing inclusive ranges with exclusive
      • Expecting a syntax error
      4. Identify the error in this Kotlin code snippet:
      for (i in 1..5)
          if (i in 3..1) println(i)
      medium
      A. The for loop syntax is incorrect
      B. The range 3..1 is invalid because start is greater than end
      C. No error, code runs fine
      D. The if condition should use 'not in' instead of 'in'

      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]
      Hint: Ranges with start > end are empty but valid [OK]
      Common 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
      5. You want to print all even numbers between 2 and 10 inclusive using a Kotlin for loop and range. Which code snippet correctly does this?
      hard
      A. for (i in 2..10 step 2) println(i)
      B. for (i in 2..10) if (i % 2 != 0) println(i)
      C. for (i in 2 until 10 step 2) println(i)
      D. for (i in 2..10 step 3) println(i)

      Solution

      1. Step 1: Understand the range and step usage

        2..10 step 2 creates numbers 2, 4, 6, 8, 10.
      2. Step 2: Check each option

        for (i in 2..10 step 2) println(i) prints 2,4,6,8,10 directly. for (i in 2..10) if (i % 2 != 0) println(i) checks i % 2 != 0 and prints odds 3,5,7,9. for (i in 2 until 10 step 2) println(i) uses until excluding 10 (2,4,6,8). for (i in 2..10 step 3) println(i) steps by 3 (2,5,8).
      3. Final Answer:

        for (i in 2..10 step 2) println(i) -> Option A
      4. Quick Check:

        Use step 2 with inclusive range for even numbers [OK]
      Hint: Use 'step 2' with '..' to get even numbers directly [OK]
      Common Mistakes:
      • Using wrong modulo condition like % 2 != 0
      • Using 'until' which excludes the end value
      • Using wrong step size like 3
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard