Enumerables VI
1.
What does the `find` method do in Ruby Enumerables?
Filters elements based on a regular expression.
Returns the first element that satisfies a given condition.
Sorts the elements in ascending order.
Counts the occurrences of a specific element.
2.
How can you transform each element in an Enumerable and collect the results in an array?
alter
transform
map
modify
3.
What does the `reject` method do in Ruby Enumerables?
Filters elements based on a regular expression.
Returns a new array with elements that do not satisfy a given condition.
Groups consecutive elements based on a given block.
Counts the occurrences of a specific element.
4.
How can you check if all elements in an Enumerable are integers?
is_integer?{|e| e}
every?{|e| e.is_integer?}
integer?{|e| e.all?}
all?{|e| e.is_a?(Integer)}
5.
What is the purpose of the `each_cons` method in Enumerables?
Counts the number of elements in the collection.
Iterates over consecutive elements in the collection.
Groups elements based on a given condition.
Transforms each element using a block.
6.
How can you sort an Enumerable in descending order?
sort.reverse
sort_by.reverse
descend
sort_desc
7.
What will be printed by the following code?
2,4
1,3,5
1,2,3,4,5
2,4,6,8,10
8.
How do you find the maximum value in an Enumerable?
max
maximum
top
high
9.
What is the output of the following code?
{0=>[1, 3, 5], 1=>[2, 4]}
{1=>[1, 3, 5], 0=>[2, 4]}
{1=>[2, 4], 0=>[1, 3, 5]}
{0=>[2, 4], 1=>[1, 3, 5]}
10.
What is the purpose of the `inject` method in Ruby Enumerables?
Combines elements into a single value by applying a binary operation.
Counts the occurrences of a specific element.
Transforms each element using a block.
Filters elements based on a given condition.
11.
How can you remove duplicate elements from an Enumerable?
remove_duplicates
unique
distinct
uniq
12.
What is the result of the following code?
10,8,6,4,2
2,4,6,8,10
1,2,3,4,5
5,4,3,2,1
13.
What does the `take_while` method do in Ruby Enumerables?
Returns elements from the beginning of the collection until the block condition is false.
Returns elements from the end of the collection until the block condition is true.
Transforms each element using a block.
Counts the occurrences of a specific element.
14.
What does the `zip` method do in Ruby Enumerables?
Creates a zip file containing the Enumerables.
Unzips an array of arrays into two separate Enumerables.
Zips elements in random order.
Combines corresponding elements from two Enumerables into an array of arrays.
15.
What will the output be for the following code?
1,2,3,4,5,1,2,3,4,5
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,1,2,3
1,2,3,4,5,1,2
16.
How do you find the first n elements in an Enumerable?
take(n)
select_first(n)
first_n
find_first(n)
17.
What is the purpose of the `each_with_index` method in Enumerables?
Transforms each element using a block.
Counts the number of elements in the collection.
Iterates over each element along with its index.
Selects elements based on a given condition.
18.
How can you find the index of the last occurrence of a specific element in an Enumerable?
rindex
last_index
find_last_index
index_last
19.
What does the `slice_after` method do in Ruby Enumerables?
Shuffles the elements randomly.
Groups consecutive elements based on a given block.
Returns the last element in the collection.
Slices the collection after a given element.
20.
What does the `chunk` method do in Ruby Enumerables?
Returns the last element in the collection.
Splits the collection into two equal parts.
Groups consecutive elements based on a given block.
Counts the occurrences of a specific element.