MCQs on Collections in Ruby | Ruby

Explore the power of Ruby collections with MCQs covering Arrays, Hashes, and common methods. Understand creation, manipulation, iteration, and key-value pair usage for better programming practices.


Topic 1: Arrays – Creation, Manipulation, and Iteration

  1. How do you create an empty array in Ruby?
    a) array = []
    b) array = {}
    c) array = ()
    d) array = new Array()
  2. Which method is used to add an element to the end of an array in Ruby?
    a) append
    b) push
    c) insert
    d) add
  3. How can you access the third element of an array arr = [10, 20, 30, 40] in Ruby?
    a) arr[2]
    b) arr[3]
    c) arr(3)
    d) arr[1]
  4. Which of the following methods removes the last element from an array?
    a) pop
    b) shift
    c) slice
    d) delete
  5. What does the map method do when applied to an array?
    a) Modifies the array in place
    b) Creates a new array based on the original array’s elements
    c) Deletes elements from the array
    d) Reverses the array
  6. How do you iterate over each element in an array in Ruby?
    a) for x in array
    b) array.each { |element| ... }
    c) array.each { element in ... }
    d) array.each()
  7. How do you remove nil values from an array?
    a) array.remove_nil
    b) array.compact
    c) array.filter_nil
    d) array.delete_nil
  8. How do you join an array of strings into a single string?
    a) array.join(", ")
    b) array.concat(", ")
    c) array.merge(", ")
    d) array.combine(", ")
  9. What method would you use to find the length of an array in Ruby?
    a) array.size
    b) array.length
    c) array.count
    d) All of the above
  10. What does the reverse method do to an array in Ruby?
    a) Reverses the order of elements
    b) Removes the last element
    c) Creates a new array
    d) Sorts the array in reverse order

Topic 2: Hashes – Key-Value Pairs and Usage

  1. How do you create a hash in Ruby with keys and values?
    a) hash = {key1 => value1, key2 => value2}
    b) hash = [key1, value1, key2, value2]
    c) hash = (key1, value1, key2, value2)
    d) hash = Hash.new(key1: value1)
  2. Which method retrieves the value associated with a given key in a hash?
    a) get
    b) fetch
    c) access
    d) retrieve
  3. How do you add a new key-value pair to an existing hash?
    a) hash.add(key, value)
    b) hash[key] = value
    c) hash.insert(key, value)
    d) hash.push(key, value)
  4. What method is used to remove a key-value pair from a hash?
    a) remove
    b) delete
    c) pop
    d) clear
  5. How do you iterate over a hash in Ruby?
    a) for key, value in hash
    b) hash.each { |key, value| ... }
    c) hash.each_pair { |key, value| ... }
    d) All of the above
  6. Which of the following methods will return an array of keys in a hash?
    a) hash.keys
    b) hash.get_keys
    c) hash.to_a
    d) hash.values
  7. How do you check if a key exists in a hash?
    a) hash.has_key?(key)
    b) hash.contains?(key)
    c) hash.key?(key)
    d) Both a and c
  8. Which method is used to merge two hashes in Ruby?
    a) merge
    b) combine
    c) union
    d) concat
  9. How can you obtain the value of a hash key, but return a default value if the key doesn’t exist?
    a) hash[key] || default_value
    b) hash.fetch(key, default_value)
    c) hash.get(key, default_value)
    d) Both a and b
  10. What is the method to retrieve all values from a hash?
    a) hash.values
    b) hash.get_values
    c) hash.value
    d) hash.find_values

Topic 3: Common Methods for Arrays and Hashes

  1. What method can you use to check if an array contains a specific element?
    a) array.include?(element)
    b) array.has?(element)
    c) array.exists?(element)
    d) array.contains?(element)
  2. Which method is used to find the index of a specific element in an array?
    a) array.find_index(element)
    b) array.index_of(element)
    c) array.locate(element)
    d) array.get_index(element)
  3. Which method returns a new array with elements sorted in ascending order?
    a) array.sort
    b) array.ascending
    c) array.order
    d) array.sort_by
  4. How do you check if a hash is empty in Ruby?
    a) hash.empty?
    b) hash.is_empty?
    c) hash.null?
    d) hash.clear?
  5. What does the flatten method do in Ruby?
    a) Combines nested arrays into a single array
    b) Removes duplicate elements from the array
    c) Converts all elements into strings
    d) Sorts the array in place
  6. How do you find the size of a hash in Ruby?
    a) hash.size
    b) hash.count
    c) hash.length
    d) All of the above
  7. How can you remove all elements from an array in Ruby?
    a) array.remove_all
    b) array.clear
    c) array.delete_all
    d) array.empty
  8. Which method would you use to combine multiple arrays in Ruby?
    a) concat
    b) merge
    c) combine
    d) append
  9. How do you convert a hash to an array of key-value pairs?
    a) hash.to_a
    b) hash.arrayize
    c) hash.keys_values
    d) hash.to_array
  10. Which method is used to check if an element exists in a hash?
    a) hash.include?(key)
    b) hash.exists?(key)
    c) hash.contains?(key)
    d) hash.has_key?(key)

Answers Table

QNoAnswer
1a) array = []
2b) push
3a) arr[2]
4a) pop
5b) Creates a new array based on the original array’s elements
6b) `array.each {
7b) array.compact
8a) array.join(", ")
9d) All of the above
10a) Reverses the order of elements
11a) hash = {key1 => value1, key2 => value2}
12b) fetch
13b) hash[key] = value
14b) delete
15d) All of the above
16a) hash.keys
17d) Both a and c
18a) merge
19d) Both a and b
20a) hash.values
21a) array.include?(element)
22a) array.find_index(element)
23a) array.sort
24a) hash.empty?
25a) Combines nested arrays into a single array
26d) All of the above
27b) array.clear
28a) concat
29a) hash.to_a
30d) hash.has_key?(key)

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