0
0
Rubyprogramming~20 mins

Loop method for infinite loops in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby 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 infinite loop with break
What is the output of this Ruby code?
Ruby
count = 0
loop do
  puts count
  count += 1
  break if count == 3
end
A
0
1
2
B
0
1
2
3
C
1
2
3
DNo output, infinite loop
Attempts:
2 left
💡 Hint
The loop stops when count reaches 3, so it prints before breaking.
Predict Output
intermediate
2:00remaining
Loop method with next keyword
What will this Ruby code print?
Ruby
i = 0
loop do
  i += 1
  next if i.even?
  puts i
  break if i >= 5
end
ANo output, infinite loop
B
2
4
6
C
1
2
3
4
5
D
1
3
5
Attempts:
2 left
💡 Hint
The next keyword skips even numbers.
🔧 Debug
advanced
2:00remaining
Identify the error in this infinite loop
What error does this Ruby code produce?
Ruby
loop do
  puts "Hello"
  break if false
end
ARuntimeError: break from proc-closure
BSyntaxError
CNo error, infinite loop printing "Hello" forever
DNameError
Attempts:
2 left
💡 Hint
The break condition is always false, so the loop never stops.
🧠 Conceptual
advanced
2:00remaining
Behavior of loop with return inside
What is the value of variable result after running this Ruby code?
Ruby
result = loop do
  break 42
end
A42
Bnil
Ctrue
DRaises an error
Attempts:
2 left
💡 Hint
The break keyword can return a value from the loop.
Predict Output
expert
3:00remaining
Output of nested loops with break and next
What is the output of this Ruby code?
Ruby
i = 0
loop do
  i += 1
  j = 0
  loop do
    j += 1
    break if j > 2
    next if j == 1
    puts "i=#{i}, j=#{j}"
  end
  break if i >= 2
end
ANo output, infinite loop
B
i=1, j=2
i=2, j=2
Ci=1, j=2
D
i=1, j=1
i=1, j=2
i=2, j=1
i=2, j=2
Attempts:
2 left
💡 Hint
The inner loop skips j=1 and breaks after j>2; outer loop runs twice.