In Ruby, advanced collections help handle and manipulate complex data structures efficiently. Learn about Iterators, Enumerable Methods, Ranges, and how to work with Nested Collections for powerful data manipulation.
Iterators and Enumerable Methods (Questions 1-10)
Which of the following methods is part of the Enumerable module in Ruby? a) each b) for c) while d) loop
What does the map method in Ruby do? a) Returns the sum of the elements in an array b) Modifies each element and returns a new array c) Filters elements based on a condition d) Counts the occurrences of an element
What is the result of calling select on an array in Ruby? a) Returns all elements b) Filters elements based on a block condition c) Returns the index of elements d) Returns the first element
Which method would you use to combine multiple elements of an array into a single value in Ruby? a) each b) reduce c) map d) select
Which of the following is the correct syntax to loop through an array using the each method? a) array.each { |element| puts element } b) array.each(element) { puts element } c) each array { |element| puts element } d) each do { |element| puts element }
In Ruby, what does the find method do when used on an array? a) Returns the index of the element b) Returns the first element that matches the condition c) Modifies the element if a condition is met d) Deletes elements that match the condition
Which Ruby method is used to check if a block is given to an enumerable method? a) block_given? b) has_block? c) check_block d) is_block?
What is the result of using the inject method in Ruby? a) It returns the first element of an array b) It accumulates a result based on a block c) It maps the array to a new one d) It sorts the array in ascending order
Which method in Ruby can be used to find the first element of an array that satisfies a given condition? a) find b) each c) map d) reject
What does the all? method do in Ruby when called on an array? a) Returns true if all elements are true b) Returns false if any element is false c) Returns true if any element is true d) Returns false if all elements are true
Working with Ranges (Questions 11-16)
How do you create a range in Ruby? a) 1..5 b) 1...5 c) Range.new(1, 5) d) All of the above
What is the difference between 1..5 and 1...5 in Ruby? a) 1..5 includes 5, 1...5 excludes 5 b) 1..5 excludes 5, 1...5 includes 5 c) Both include 5 d) Both exclude 5
How can you check if a value is included in a range in Ruby? a) range.include?(value) b) range.contains(value) c) range.has(value) d) range.included?(value)
Which of the following methods can be used to iterate through a range in Ruby? a) each b) map c) times d) All of the above
What will be the output of the following Ruby code?rubyCopy code(1..3).to_a a) [1, 2, 3] b) [1, 2] c) [1, 3] d) Error
What does the step method do in Ruby for ranges? a) Skips over values in the range by a certain step b) Returns the first element c) Returns all even values in the range d) Stops iteration after the first element
Nested Collections and Complex Data Structures (Questions 17-30)
How do you create a nested array in Ruby? a) array = [[1, 2], [3, 4]] b) array = [1, [2, 3], 4] c) array = [[1, 2], [3, 4], 5] d) All of the above
What is the method to access an element from a nested array in Ruby? a) array[1][2] b) array(1)(2) c) array{1, 2} d) array[1, 2]
How do you access the first element of a nested array in Ruby? a) array[0][0] b) array[0] c) array[1] d) array[0, 0]
In Ruby, which method can be used to flatten a nested array? a) flatten b) flatten! c) flatten_all d) Both a and b
How do you create a hash with arrays as values in Ruby? a) hash = {a: [1, 2, 3]} b) hash = {1 => [4, 5, 6]} c) hash = {:key => [7, 8, 9]} d) All of the above
How can you retrieve a specific value from a nested hash in Ruby? a) hash[:key][:subkey] b) hash[:subkey][:key] c) hash[1][2] d) hash[0][1]
What is the method to merge two hashes in Ruby? a) merge b) combine c) append d) union
In Ruby, which of the following is used to delete a key-value pair from a hash? a) delete b) remove c) pop d) shift
Which Ruby method can be used to check if a hash contains a specific key? a) key? b) has_key? c) include? d) All of the above
What is the result of calling uniq on an array in Ruby? a) Removes duplicate elements b) Returns the sum of elements c) Reverses the array d) Sorts the array
How do you iterate through each key-value pair in a hash in Ruby? a) hash.each b) hash.each_key c) hash.each_value d) All of the above
How can you check the size of a hash in Ruby? a) hash.size b) hash.length c) hash.count d) All of the above
What is the result of calling collect on a hash in Ruby? a) Returns the modified hash b) Iterates over the values and returns a new collection c) Filters the keys based on a condition d) Deletes the first key-value pair
What will the following Ruby code output?rubyCopy codenested_hash = {a: {b: {c: 1}}} nested_hash.dig(:a, :b, :c) a) 1 b) nil c) {c: 1} d) Error
Answer Key
QNo
Answer (Option with Text)
1
a) each
2
b) Modifies each element and returns a new array
3
b) Filters elements based on a block condition
4
b) reduce
5
a) `array.each {
6
b) Returns the first element that matches the condition
7
a) block_given?
8
b) It accumulates a result based on a block
9
a) find
10
a) Returns true if all elements are true
11
d) All of the above
12
a) 1..5 includes 5, 1...5 excludes 5
13
a) range.include?(value)
14
d) All of the above
15
a) [1, 2, 3]
16
a) Skips over values in the range by a certain step
17
d) All of the above
18
a) array[1][2]
19
a) array[0][0]
20
d) Both a and b
21
d) All of the above
22
a) hash[:key][:subkey]
23
a) merge
24
a) delete
25
d) All of the above
26
a) Removes duplicate elements
27
d) All of the above
28
d) All of the above
29
b) Iterates over the values and returns a new collection