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?<br>
for (i in 1..3) {
println(i)
}It will print:<br>1<br>2<br>3<br>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?✗ Incorrect
1..5 creates a range including both 1 and 5.What is the result of
3 in 1..5?✗ Incorrect
3 is inside the range 1 to 5, so it returns true.
Which operator checks if a value is NOT in a range?
✗ Incorrect
The
!in operator checks if a value is outside a range.What will this print?<br>
for (c in 'a'..'c') println(c)✗ Incorrect
The loop prints characters from 'a' to 'c' inclusive.
Is
10 in 1..9 true or false?✗ Incorrect
10 is not in the range 1 to 9, so it returns false.
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.