MCQs on Advanced Collections | Ruby

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)

  1. Which of the following methods is part of the Enumerable module in Ruby?
    a) each
    b) for
    c) while
    d) loop
  2. 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
  3. 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
  4. 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
  5. 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 }
  6. 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
  7. 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?
  8. 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
  9. 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
  10. 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)

  1. How do you create a range in Ruby?
    a) 1..5
    b) 1...5
    c) Range.new(1, 5)
    d) All of the above
  2. 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
  3. 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)
  4. 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
  5. 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
  6. 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)

  1. 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
  2. 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]
  3. 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]
  4. In Ruby, which method can be used to flatten a nested array?
    a) flatten
    b) flatten!
    c) flatten_all
    d) Both a and b
  5. 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
  6. 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]
  7. What is the method to merge two hashes in Ruby?
    a) merge
    b) combine
    c) append
    d) union
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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

QNoAnswer (Option with Text)
1a) each
2b) Modifies each element and returns a new array
3b) Filters elements based on a block condition
4b) reduce
5a) `array.each {
6b) Returns the first element that matches the condition
7a) block_given?
8b) It accumulates a result based on a block
9a) find
10a) Returns true if all elements are true
11d) All of the above
12a) 1..5 includes 5, 1...5 excludes 5
13a) range.include?(value)
14d) All of the above
15a) [1, 2, 3]
16a) Skips over values in the range by a certain step
17d) All of the above
18a) array[1][2]
19a) array[0][0]
20d) Both a and b
21d) All of the above
22a) hash[:key][:subkey]
23a) merge
24a) delete
25d) All of the above
26a) Removes duplicate elements
27d) All of the above
28d) All of the above
29b) Iterates over the values and returns a new collection
30a) 1

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top