Ruby Hashes (Dictionary) Quiz
40 comprehensive questions exploring Ruby hash manipulation, key-value operations, and dictionary data structures — with 16 code examples covering hash creation, symbol vs string keys, iteration, and default values in this cpp quiz.
Question 1
How do you create a basic hash in Ruby?
Question 2
What is the difference between symbol keys and string keys in Ruby hashes?
Question 3
When storing user configuration data where keys are known constants like database settings, which key type should you prefer?
config = { host: 'localhost', port: 5432, database: 'myapp' }Question 4
How do you access values in a Ruby hash?
Question 5
What does the fetch method do compared to [] access?
hash = { a: 1, b: 2 }
value = hash.fetch(:c, 'default')Question 6
In a web application processing JSON API responses where keys come from external data, how should you handle key access?
Question 7
How do you add or update values in a hash?
Question 8
What does the delete method do in Ruby hashes?
hash = { a: 1, b: 2, c: 3 }
removed = hash.delete(:b)Question 9
How do you iterate over all key-value pairs in a hash?
Question 10
When processing user profile data where you need to display all attributes, how should you iterate through the hash?
Question 11
What is a hash default value and how does it work?
hash = Hash.new('default')
value = hash[:missing] # 'default'Question 12
How do you create a hash with a default block instead of a default value?
Question 13
What does the merge method do when combining hashes?
hash1 = { a: 1, b: 2 }
hash2 = { b: 3, c: 4 }
combined = hash1.merge(hash2)Question 14
In a configuration management system where you need to merge default settings with user overrides, which merge method should you use?
Question 15
How do you check if a key exists in a hash?
Question 16
What does the select method do when working with hashes?
hash = { a: 1, b: 2, c: 3, d: 4 }
evens = hash.select { |k, v| v.even? }Question 17
When implementing a caching system where you need to store computation results with dynamic keys, how should you handle cache misses?
Question 18
What is the difference between merge and merge! methods?
Question 19
How do you iterate over only the keys of a hash?
hash = { a: 1, b: 2, c: 3 }
hash.each_key { |key| puts key }Question 20
In a data validation system where you need to check that all required fields are present in user input, how should you validate hash keys?
Question 21
What does the reject method do in Ruby hashes?
Question 22
When working with API responses that contain nested hash structures, how should you safely access deeply nested values?
Question 23
How do you transform all values in a hash while keeping the same keys?
hash = { a: 1, b: 2, c: 3 }
doubled = hash.transform_values { |v| v * 2 }Question 24
What does the invert method do to a hash?
Question 25
In a multilingual application where you need to create reverse mappings from translated strings back to internal keys, how should you handle the hash inversion?
Question 26
How do you iterate over only the values of a hash?
hash = { a: 1, b: 2, c: 3 }
hash.each_value { |value| puts value }Question 27
When implementing a word frequency counter where you need to count occurrences of each word, how should you handle the counting logic?
Question 28
What does the compact method do when applied to hashes?
Question 29
How do you create a hash from two arrays, one with keys and one with values?
keys = [:a, :b, :c]
values = [1, 2, 3]
hash = keys.zip(values).to_hQuestion 30
In a data processing pipeline where you need to filter hash entries based on complex conditions involving both keys and values, which method should you use?
Question 31
What does the keys method return for a hash?
Question 32
When implementing a configuration system with environment-specific overrides, how should you merge multiple configuration hashes?
Question 33
How do you check if a hash is empty?
hash = {}
empty = hash.empty?Question 34
What does the values method return for a hash?
Question 35
In a caching system where you need to limit cache size by removing least recently used items, how should you implement the cache structure?
Question 36
What does the slice method do when used with hashes?
hash = { a: 1, b: 2, c: 3, d: 4 }
subset = hash.slice(:a, :c)Question 37
When processing form data where multiple values can have the same name, how should you handle the resulting hash structure?
Question 38
How do you sort a hash by its keys?
Question 39
What does the fetch_values method do?
hash = { a: 1, b: 2, c: 3 }
values = hash.fetch_values(:a, :c)Question 40
Considering Ruby's hash handling ecosystem as a whole, what fundamental advantage do Ruby hashes provide compared to basic key-value stores in other languages?
