What does the downTo keyword do in a Kotlin for loop?
It creates a range that counts backwards from a higher number down to a lower number.
Click to reveal answer
beginner
How do you make a Kotlin for loop skip numbers when counting?
Use the step keyword to specify how many numbers to skip each time.
Click to reveal answer
intermediate
Write a Kotlin for loop that counts down from 10 to 1, stepping by 2.
for (i in 10 downTo 1 step 2) {
println(i)
}
Click to reveal answer
intermediate
What will this Kotlin code print?
for (i in 5 downTo 1 step 2) {
println(i)
}
It will print:
5
3
1
Because it counts down from 5 to 1, skipping every second number.
Click to reveal answer
beginner
Can you use step with a normal increasing range in Kotlin?
Yes! For example, for (i in 1..10 step 3) counts from 1 to 10, jumping 3 numbers each time.
Click to reveal answer
What does for (i in 10 downTo 1 step 3) do?
ACounts up from 10 to 1, jumping 3 numbers each time
BCounts up from 1 to 10, jumping 3 numbers each time
CCounts down from 1 to 10, jumping 3 numbers each time
DCounts down from 10 to 1, jumping 3 numbers each time
✗ Incorrect
The downTo keyword makes the loop count backwards, and step 3 makes it jump 3 numbers each time.
Which keyword lets you skip numbers in a Kotlin for loop?
Askip
Bjump
Cstep
DdownTo
✗ Incorrect
step controls how many numbers the loop skips each time.
What will for (i in 1..5 step 2) print?
A1 2 3 4 5
B1 3 5
C5 3 1
D2 4
✗ Incorrect
It counts up from 1 to 5, stepping by 2, so it prints 1, 3, and 5.
What is the output of this code?
for (i in 3 downTo 1) {
println(i)
}
A3 2 1
BError
C1 3
D1 2 3
✗ Incorrect
The loop counts down from 3 to 1, printing 3, then 2, then 1.
Can step be used with downTo in Kotlin?
AYes, it works with both increasing and decreasing ranges
BNo, only with increasing ranges
COnly with <code>until</code>
DOnly with <code>rangeTo</code>
✗ Incorrect
step works with both downTo (decreasing) and normal increasing ranges.
Explain how to use downTo and step together in a Kotlin for loop.
Think about counting backwards and skipping numbers.
You got /3 concepts.
Describe what happens when you use step in a Kotlin for loop with an increasing range.
Imagine counting forward but only saying every other number.
You got /3 concepts.
Practice
(1/5)
1. What does the downTo keyword do in a Kotlin for loop?
easy
A. It makes the loop count forwards from a lower number to a higher number.
B. It makes the loop count backwards from a higher number to a lower number.
C. It skips numbers in the loop by a fixed step.
D. It stops the loop immediately.
Solution
Step 1: Understand the purpose of downTo
The downTo keyword in Kotlin is used to create a range that counts backwards, from a higher number down to a lower number.
Step 2: Compare with other options
It makes the loop count forwards from a lower number to a higher number. describes counting forwards, It skips numbers in the loop by a fixed step. skipping numbers by step, and It stops the loop immediately. stopping the loop immediately, none of which downTo does.
Final Answer:
It makes the loop count backwards from a higher number to a lower number. -> Option B
Quick Check:
downTo = count backwards [OK]
Hint: Remember: downTo means counting downwards [OK]
Common Mistakes:
Confusing downTo with counting forwards
Thinking downTo skips numbers
Assuming downTo stops the loop
2. Which of the following is the correct syntax to loop from 10 down to 1, stepping by 2 in Kotlin?
easy
A. for (i in 10..1 step 2) { println(i) }
B. for (i in 1 downTo 10 step 2) { println(i) }
C. for (i in 10 downTo 1 step 2) { println(i) }
D. for (i in 10 downTo 1 by 2) { println(i) }
Solution
Step 1: Identify correct downTo and step syntax
The correct syntax to count down with steps is for (i in start downTo end step stepValue). for (i in 10 downTo 1 step 2) { println(i) } matches this.
Step 2: Check other options for errors
for (i in 1 downTo 10 step 2) { println(i) } has start < end, resulting in empty range. for (i in 10..1 step 2) { println(i) } uses .. which doesn't support down with step correctly. for (i in 10 downTo 1 by 2) { println(i) } uses by instead of step, which is incorrect.
Final Answer:
for (i in 10 downTo 1 step 2) { println(i) } -> Option C
Quick Check:
Correct syntax = for (i in 10 downTo 1 step 2) { println(i) } [OK]
Hint: Use downTo with step, not by [OK]
Common Mistakes:
Using by instead of step
Counting down with .. range
Reversing start and end values
3. What is the output of the following Kotlin code?
for (i in 8 downTo 2 step 3) {
print("$i ")
}
medium
A. 8 5 2
B. 8 6 4 2
C. 8 5 3
D. 8 5 2 1
Solution
Step 1: Determine the loop values
The loop starts at 8, counts down to 2, stepping by 3. So values are 8, then 8-3=5, then 5-3=2.
Step 2: Confirm the loop stops at 2
Since 2 is the end value and included, the loop prints 8, 5, and 2.
Final Answer:
8 5 2 -> Option A
Quick Check:
8, 5, 2 with step 3 downTo 2 = 8 5 2 [OK]
Hint: Subtract step each time, stop at end value [OK]
Common Mistakes:
Including numbers less than end value
Using wrong step size
Confusing downTo with ..
4. Identify the error in this Kotlin loop:
for (i in 5 downTo 1 step 0) {
println(i)
}
medium
A. The downTo keyword is misspelled.
B. There is no error; the code runs fine.
C. The loop should use .. instead of downTo.
D. Step value cannot be zero; it causes a runtime error.
Solution
Step 1: Analyze the step value
The step value is 0, which is invalid and causes a runtime error (IllegalArgumentException).
Step 2: Check other options
downTo is spelled correctly, and using .. would count upwards, not downwards. So options A, C, and D are incorrect.
Final Answer:
Step value cannot be zero; it causes a runtime error. -> Option D
Quick Check:
Step must be positive and non-zero [OK]
Hint: Step cannot be zero; it must be positive [OK]
Common Mistakes:
Using zero or negative step incorrectly
Confusing downTo with ..
Ignoring infinite loop risk
5. You want to print all odd numbers from 15 down to 1 using a Kotlin for loop with downTo and step, without using an if statement. Which code snippet correctly does this?
hard
A. for (i in 15 downTo 1 step 2) { println(i) }
B. for (i in 15 downTo 1 step 1) { if (i % 2 == 1) println(i) }
C. for (i in 15 downTo 1 step 3) { println(i) }
D. for (i in 14 downTo 1 step 2) { println(i) }
Solution
Step 1: Understand the goal
We want to print odd numbers from 15 down to 1. Starting at 15 (which is odd) and stepping by 2 will print all odd numbers downwards.
Step 2: Evaluate options
for (i in 15 downTo 1 step 2) { println(i) } starts at 15 and steps by 2 down to 1, printing 15, 13, 11, ..., 1. for (i in 15 downTo 1 step 1) { if (i % 2 == 1) println(i) } uses step 1 with an if filter (disallowed). for (i in 15 downTo 1 step 3) { println(i) } steps by 3, skipping some odds. for (i in 14 downTo 1 step 2) { println(i) } starts at 14 (even), printing evens.
Final Answer:
for (i in 15 downTo 1 step 2) { println(i) } -> Option A
Quick Check:
Start odd, step 2 downTo 1 = odd numbers [OK]
Hint: Start at odd number, step 2 downTo 1 for odd numbers [OK]
Common Mistakes:
Starting at even number when printing odds
Using wrong step size
Filtering inside loop instead of stepping
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.