General Ruby XI
1.
In Ruby, what is the purpose of the `has_key?` method for hashes?
Sorts the keys in the hash
Counts the number of keys in the hash
Retrieves the value associated with a key
Checks if a hash contains a specific key
2.
What is the result of `puts 5 / 2` in Ruby?
2
2.5
2.0
Error
3.
What is the purpose of the `push` method when used with an array in Ruby?
Reverses the order of the array
Removes the last element from the array
Sorts the array in ascending order
Adds one or more elements to the end of the array
4.
In Ruby, what is the primary benefit of using a Set instead of an Array for a collection of unique elements?
Sets can hold elements of different data types
Sets allow for faster element retrieval
Sets are simpler to use than arrays
Sets ensure that all elements are unique, so there are no duplicates
5.
How do you access the first element of an array in Ruby?
array[1]
array.first
array.access_first
array.get(1)
6.
How do you remove a key-value pair from a hash in Ruby?
hash.delete_pair(key)
hash.remove(key)
hash.remove_pair(key)
hash.delete(key)
7.
Which data structure in Ruby is often used for implementing first-in-first-out (FIFO) behavior?
Hash
Stack
Array
Queue
8.
What is the purpose of the `sort` method on an array in Ruby?
Reverses the order of elements in the array
Sorts the elements in descending order
Sorts the elements in ascending order
Shuffles the elements randomly
9.
How do you check if a string contains a specific substring in Ruby?
string.has('substring')
substring.contains?(string)
string.include?('substring')
substring.exists_in(string)
10.
In Ruby, what is the purpose of the `class` keyword?
Defines a module
Defines a class
Defines a method
Creates an instance of a class
11.
How do you iterate over the key-value pairs in a hash in Ruby?
hash.each
hash.keys.each
hash.each_pair
A & C
12.
In Ruby, what is the purpose of the `times` method on an integer?
Multiplies the integer by a specified value
Returns a new array with repeated elements
Executes a block of code a specified number of times
Raises the integer to a specified power
13.
What is the primary purpose of the `attr_accessor` method in Ruby?
Creates both getter and setter methods for an instance variable
Creates a read-only attribute for an instance variable
Creates a write-only attribute for an instance variable
Creates a class-level attribute
14.
How do you add a key-value pair to a hash in Ruby?
hash.put(key, value)
hash.add(key, value)
hash.insert(key, value)
hash[key] = value
15.
What is the primary difference between an array and a hash in Ruby?
Arrays can store only numbers; hashes can store any data type
Arrays are indexed by integers; hashes are indexed by keys
Arrays are sorted in ascending order; hashes are not sorted
Arrays can have duplicate elements; hashes cannot have duplicate keys
16.
How do you remove a specific element from an array in Ruby?
array.delete(element)
array.remove(element)
array.find_and_remove(element)
array.extract(element)
17.
What is the result of `array.uniq` in Ruby?
A new array with duplicate elements removed
An error
The original array with all elements
An empty array
18.
In Ruby, what does the `!` (bang) in a method name often indicate?
The end of the method
A safe or non-destructive version of the method
A dangerous or destructive version of the method
A block for the method
19.
How do you convert an array to a string in Ruby?
array.to_s
array.join
convert_to_string(array)
stringify(array)
20.
What is the purpose of the `<<` operator in Ruby when used with an array?
Replaces an element in the middle of the array
Prepends an element to the beginning of the array
Removes the last element from the array
Appends an element to the end of the array
21.
In a Ruby hash, what is the role of keys and values?
Both keys and values store data
Keys store the actual data; values are used to access keys
Keys are used to access values; values store the actual data
Keys are used for iteration; values are used for searching