Loops I
1.
What does the 'map' method do in Ruby?
Modifies each element of an array
Iterates over each element of an array
Creates a new array with the results of applying a block
Removes elements from an array
2.
What is the purpose of the 'redo' statement in Ruby?
Restarts the current iteration
Exits the loop
Skips the current iteration
Jumps to the specified label
3.
What is the output of the following code?
"5.times do |i| puts i end"
1,2,3,4,5
0,1,2,3,4
2,3,4,5,6
24
4.
In Ruby, how can you iterate over the characters of a string?
for
each
while
iterate
5.
What is the output of the following code?
"(1..5).each do |i| puts i * 2 end"
2,4,6,8,10
1,2,3,4,5
120
10
6.
How do you exit a loop prematurely in Ruby?
break
exit
continue
return
7.
How can you iterate over each element of an array in Ruby?
for
each
while
iterate
8.
What keyword is used for an infinite loop in Ruby?
while
for
loop
until
9.
In Ruby, which loop is guaranteed to execute at least once?
for
until
while
do-while
10.
What does the 'next' keyword do in a Ruby loop?
Skips the current iteration
Exits the loop
Restarts the loop
Jumps to the specified label
11.
Which loop in Ruby is used for iterating over a range of values?
while
for
loop
each
12.
Which method is used to repeat a block of code a specified number of times in Ruby?
loop
for
times
repeat
13.
What does the 'unless' keyword do in Ruby?
Acts like 'if' but with a negative condition
Acts like 'if' with a positive condition
Acts like 'else' with a negative condition
Acts like 'else' with a positive condition
14.
In Ruby, what is the purpose of the 'retry' statement?
Restarts the entire program
Restarts the current iteration of the loop
Exits the loop
Jumps to the specified label
15.
Which loop in Ruby is used for iterating over a collection of elements?
for
while
loop
each
16.
In Ruby, what is the purpose of the 'break' statement in a loop?
Exits the loop prematurely
Restarts the loop
Skips the current iteration
Jumps to the specified label
17.
How is the 'while' loop different from the 'until' loop in Ruby?
'while' executes when the condition is true, 'until' when false
'while' executes when the condition is false, 'until' when true
There is no difference
They are used for different types of data
18.
What does the 'select' method do in Ruby?
Modifies each element of an array
Iterates over each element of an array
Creates a new array with elements that satisfy a condition
Removes elements from an array
19.
What is the purpose of the 'redo' statement in Ruby?
Restarts the current iteration
Exits the loop
Skips the current iteration
Jumps to the specified label
20.
How do you iterate over key-value pairs in a hash in Ruby?
for
each
while
iterate