0
0
Rubyprogramming~20 mins

Times method for counted repetition in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Times Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Times with Block Variable
What is the output of this Ruby code?
Ruby
3.times do |i|
  puts i * 2
end
A
1
2
3
B
0
1
2
C
0
2
4
D
2
4
6
Attempts:
2 left
💡 Hint
Remember, the block variable starts at 0 and goes up to one less than the count.
Predict Output
intermediate
2:00remaining
Times Method Without Block Variable
What will this Ruby code print?
Ruby
5.times { puts "Hello" }
A
Hello
Hello
Hello
Hello
Hello
B
0
1
2
3
4
C
Hello5
DError: block variable missing
Attempts:
2 left
💡 Hint
If no block variable is given, the block still runs the specified number of times.
🔧 Debug
advanced
2:00remaining
Identify the Error in Times Usage
What error does this Ruby code produce?
Ruby
4.times do
  puts i
end
ATypeError: no implicit conversion of nil into Integer
BNameError: undefined local variable or method `i' for main:Object
CNo output, runs silently
DSyntaxError: unexpected end-of-input
Attempts:
2 left
💡 Hint
Check if the block variable is defined or accessible inside the block.
Predict Output
advanced
2:00remaining
Times Method with Return Value
What is the value of variable result after running this Ruby code?
Ruby
result = 3.times.map { |i| i * 3 }
AError: undefined method `map' for Integer
B3
C[3, 6, 9]
D[0, 3, 6]
Attempts:
2 left
💡 Hint
Check the return type of times and whether it supports map.
Predict Output
expert
2:00remaining
Complex Times Loop with Conditional Output
What is the output of this Ruby code?
Ruby
5.times do |i|
  if i.even?
    print i
  else
    print "-"
  end
end
A0-2-4-
B024
C0-1-2-3-4-
D0-2-4
Attempts:
2 left
💡 Hint
Check how even numbers and odd numbers are printed inside the loop.