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.
array = []array = {}array = ()array = new Array()appendpushinsertaddarr = [10, 20, 30, 40] in Ruby?arr[2]arr[3]arr(3)arr[1]popshiftslicedeletemap method do when applied to an array?for x in arrayarray.each { |element| ... }array.each { element in ... }array.each()array.remove_nilarray.compactarray.filter_nilarray.delete_nilarray.join(", ")array.concat(", ")array.merge(", ")array.combine(", ")array.sizearray.lengtharray.countreverse method do to an array in Ruby?hash = {key1 => value1, key2 => value2}hash = [key1, value1, key2, value2]hash = (key1, value1, key2, value2)hash = Hash.new(key1: value1)getfetchaccessretrievehash.add(key, value)hash[key] = valuehash.insert(key, value)hash.push(key, value)removedeletepopclearfor key, value in hashhash.each { |key, value| ... }hash.each_pair { |key, value| ... }hash.keyshash.get_keyshash.to_ahash.valueshash.has_key?(key)hash.contains?(key)hash.key?(key)mergecombineunionconcathash[key] || default_valuehash.fetch(key, default_value)hash.get(key, default_value)hash.valueshash.get_valueshash.valuehash.find_valuesarray.include?(element)array.has?(element)array.exists?(element)array.contains?(element)array.find_index(element)array.index_of(element)array.locate(element)array.get_index(element)array.sortarray.ascendingarray.orderarray.sort_byhash.empty?hash.is_empty?hash.null?hash.clear?flatten method do in Ruby?hash.sizehash.counthash.lengtharray.remove_allarray.cleararray.delete_allarray.emptyconcatmergecombineappendhash.to_ahash.arrayizehash.keys_valueshash.to_arrayhash.include?(key)hash.exists?(key)hash.contains?(key)hash.has_key?(key)| QNo | Answer |
|---|---|
| 1 | a) array = [] |
| 2 | b) push |
| 3 | a) arr[2] |
| 4 | a) pop |
| 5 | b) Creates a new array based on the original array’s elements |
| 6 | b) `array.each { |
| 7 | b) array.compact |
| 8 | a) array.join(", ") |
| 9 | d) All of the above |
| 10 | a) Reverses the order of elements |
| 11 | a) hash = {key1 => value1, key2 => value2} |
| 12 | b) fetch |
| 13 | b) hash[key] = value |
| 14 | b) delete |
| 15 | d) All of the above |
| 16 | a) hash.keys |
| 17 | d) Both a and c |
| 18 | a) merge |
| 19 | d) Both a and b |
| 20 | a) hash.values |
| 21 | a) array.include?(element) |
| 22 | a) array.find_index(element) |
| 23 | a) array.sort |
| 24 | a) hash.empty? |
| 25 | a) Combines nested arrays into a single array |
| 26 | d) All of the above |
| 27 | b) array.clear |
| 28 | a) concat |
| 29 | a) hash.to_a |
| 30 | d) hash.has_key?(key) |