Bird
Raised Fist0
Rubyprogramming~20 mins

Upto and downto methods in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Upto and Downto Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of using upto method
What is the output of the following Ruby code?
Ruby
result = []
5.upto(7) { |i| result << i }
puts result.join(",")
A5,6,7
B7,6,5
C5,7
D6,7
Attempts:
2 left
💡 Hint
Remember, upto counts from the starting number up to the ending number, including both.
❓ Predict Output
intermediate
2:00remaining
Output of using downto method
What will this Ruby code print?
Ruby
result = []
3.downto(1) { |i| result << i }
puts result.join("-")
A1-2-3
B3-2-1
C3-1
D2-1
Attempts:
2 left
💡 Hint
Downto counts down from the starting number to the ending number, including both.
🧠 Conceptual
advanced
2:00remaining
Understanding return values of upto and downto
What is the return value of the following Ruby code snippet? ```ruby result = 1.upto(3) { |i| puts i } puts result ```
A
1
2
3
nil
B
1
2
3
3
C
1
2
3
1
D
1
2
3
Enumerator
Attempts:
2 left
💡 Hint
The upto method returns the receiver after iteration.
❓ Predict Output
advanced
2:00remaining
Output of combined upto and downto
What is the output of this Ruby code?
Ruby
output = []
2.upto(3) { |i| output << i }
3.downto(2) { |i| output << i }
puts output.join(",")
A2,3,3,2
B2,3,2,3
C3,2,2,3
D3,2,3,2
Attempts:
2 left
💡 Hint
The first loop counts up from 2 to 3, the second counts down from 3 to 2.
❓ Predict Output
expert
2:00remaining
Counting iterations with upto and downto
How many times will the block run in this Ruby code?
Ruby
count = 0
10.downto(7) { count += 1 }
puts count
A3
B10
C7
D4
Attempts:
2 left
💡 Hint
Count how many numbers are between 10 and 7, including both.

Practice

(1/5)
1. What does the Ruby method 5.upto(8) do?
easy
A. Counts from 5 down to 8, excluding 8
B. Counts from 8 down to 5, including both 8 and 5
C. Counts from 5 up to 8, including both 5 and 8
D. Counts from 8 up to 5, excluding 5

Solution

  1. Step 1: Understand the upto method

    The upto method counts upwards from the starting number to the ending number, including both.
  2. Step 2: Apply to the example 5.upto(8)

    This means it counts 5, 6, 7, 8 in order.
  3. Final Answer:

    Counts from 5 up to 8, including both 5 and 8 -> Option C
  4. Quick Check:

    upto counts up = A [OK]
Hint: Remember: upto counts up, downto counts down [OK]
Common Mistakes:
  • Confusing upto with downto
  • Thinking it excludes the last number
  • Assuming it counts down instead of up
2. Which of the following is the correct syntax to count down from 10 to 7 using downto in Ruby?
easy
A. 10.downto(7) { |i| puts i }
B. 10.upto(7) { |i| puts i }
C. 7.downto(10) { |i| puts i }
D. 7.upto(10) { |i| puts i }

Solution

  1. Step 1: Identify correct method for counting down

    To count down from 10 to 7, use downto starting at 10 and ending at 7.
  2. Step 2: Check syntax correctness

    10.downto(7) { |i| puts i } correctly counts down and prints each number.
  3. Final Answer:

    10.downto(7) { |i| puts i } -> Option A
  4. Quick Check:

    Count down syntax = C [OK]
Hint: downto starts at bigger number, ends at smaller [OK]
Common Mistakes:
  • Using upto instead of downto for counting down
  • Reversing start and end numbers
  • Missing the block syntax
3. What is the output of this Ruby code?
sum = 0
3.upto(5) { |i| sum += i }
puts sum
medium
A. 12
B. 15
C. 9
D. Error

Solution

  1. Step 1: Understand the loop range

    3.upto(5) iterates over 3, 4, 5.
  2. Step 2: Calculate sum of values

    sum = 0 + 3 + 4 + 5 = 12.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    3 + 4 + 5 = 12 [OK]
Hint: Add all numbers from start to end inclusive [OK]
Common Mistakes:
  • Adding only start and end numbers
  • Confusing upto with downto
  • Forgetting to include the last number
4. Identify the error in this Ruby code:
5.upto(3) { |i| puts i }
medium
A. Syntax error due to wrong method usage
B. No output because start is greater than end
C. Infinite loop occurs
D. Outputs numbers from 5 down to 3

Solution

  1. Step 1: Analyze the upto method behavior

    upto counts up from start to end. Here start=5, end=3, which is invalid for counting up.
  2. Step 2: Determine output

    Since 5 is already greater than 3, the block does not run and no output is produced.
  3. Final Answer:

    No output because start is greater than end -> Option B
  4. Quick Check:

    upto with start > end = no output [OK]
Hint: upto does nothing if start > end, no error [OK]
Common Mistakes:
  • Expecting output when start > end
  • Thinking it counts down automatically
  • Assuming syntax error occurs
5. Using downto, how can you create an array of even numbers counting down from 10 to 2?
hard
A. arr = [] 2.upto(10) { |i| arr << i if i.even? }
B. arr = [] 2.downto(10) { |i| arr << i if i.even? }
C. arr = [] 10.upto(2) { |i| arr << i if i.even? }
D. arr = [] 10.downto(2) { |i| arr << i if i.even? }

Solution

  1. Step 1: Choose correct method for counting down

    To count down from 10 to 2, use downto starting at 10 and ending at 2.
  2. Step 2: Filter even numbers inside the block

    Use if i.even? to add only even numbers to the array.
  3. Final Answer:

    arr = [] 10.downto(2) { |i| arr << i if i.even? } -> Option D
  4. Quick Check:

    downto + even check = B [OK]
Hint: Use downto with condition inside block to filter [OK]
Common Mistakes:
  • Using upto instead of downto for counting down
  • Reversing start and end numbers
  • Not filtering even numbers correctly
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