0
0
Rubyprogramming~20 mins

For loop (rarely used in Ruby) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby For Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple for loop
What is the output of this Ruby code using a for loop?
Ruby
for i in 1..3
  puts i * 2
end
A
2
4
6
B
1
2
3
C
1
4
9
DError: SyntaxError
Attempts:
2 left
💡 Hint
Remember the for loop iterates over the range 1 to 3 and multiplies each by 2.
Predict Output
intermediate
2:00remaining
For loop with array iteration
What will this Ruby code print?
Ruby
arr = ['a', 'b', 'c']
for letter in arr
  print letter
end
A['a', 'b', 'c']
BError: undefined method 'print'
Ca b c
Dabc
Attempts:
2 left
💡 Hint
The print method outputs without newlines or spaces.
Predict Output
advanced
2:00remaining
For loop with break statement
What is the output of this Ruby code?
Ruby
for i in 1..5
  break if i > 3
  puts i
end
A
1
2
3
4
5
B
1
2
C
1
2
3
DError: break outside loop
Attempts:
2 left
💡 Hint
The break stops the loop when i is greater than 3.
Predict Output
advanced
2:00remaining
For loop variable scope
What is the value of variable x after this Ruby code runs?
Ruby
x = 10
for x in 1..3
  # loop body empty
end
puts x
AError: variable scope conflict
B3
C1
D10
Attempts:
2 left
💡 Hint
In Ruby, the for loop variable reassigns the outer variable.
🧠 Conceptual
expert
2:00remaining
Why is for loop rarely used in Ruby?
Which reason best explains why the for loop is rarely used in Ruby?
ABecause Ruby prefers using iterators like each which have better variable scope handling
BBecause for loops do not support ranges in Ruby
CBecause for loops are slower than while loops in Ruby
DBecause for loops cause syntax errors in modern Ruby versions
Attempts:
2 left
💡 Hint
Think about how Ruby handles variables inside loops.