General Ruby VIII
1.
In Ruby, what is the purpose of the `zip` method on arrays?
Combines corresponding elements of arrays into subarrays
Reverses the order of elements in an array
Calculates the product of corresponding elements in arrays
Returns the intersection of arrays
2.
In Ruby, how do you concatenate two arrays?
array1.join(array2)
array1.concat(array2)
array1 + array2
array1.merge(array2)
3.
What is the output of `puts (1..5).to_a.join('-')` in Ruby?
1-2-3-4-5
12345
Error
An array containing numbers from 1 to 5
4.
How do you convert a symbol to a string in Ruby?
stringify(symbol)
string.symbolize
convert_to_string(symbol)
symbol.to_s
5.
What is the purpose of the `clear` method on an array in Ruby?
Sorts the array in ascending order
Removes all elements from the array
Reverses the order of the array
Returns the first element of the array
6.
How do you define a method that can take a variable number of arguments in Ruby?
def method_name(*args)
def method_name(args...)
def method_name(parameters)
def method_name(param1, param2 = default_value)
7.
What is the purpose of the `each_with_index` method in Ruby?
Sorts the elements in a collection in descending order
Iterates over each element in a collection and provides the index of the element
Returns the sum of all elements in a collection
Filters out elements in a collection based on a condition
8.
How do you remove the last element from an array in Ruby?
array.last
array.delete_last
array.pop
remove_last(array)
9.
What is the result of `"ruby".reverse` in Ruby?
ybur
ruby
yb
Error
10.
How do you check if a string ends with a specific substring in Ruby?
check_end(string, 'substring')
ends_with(string, 'substring')
string.end_with?('substring')
string.ends?('substring')
11.
What is the purpose of the `reject` method on an array in Ruby?
Sorts the array in ascending order
Returns a new array with elements that do not satisfy a condition
Reverses the order of the array
Returns the first element of the array
12.
In Ruby, what does the `<<` operator do on a string?
Prepends another string to the beginning of the original string
Appends another string to the end of the original string
Concatenates two strings
Removes the last character from the string
13.
How do you round a floating-point number to the nearest integer in Ruby?
number.floor
number.round
number.ceil
round(number)
14.
What is the result of `puts [1, 2, 3, 4].sample(2)` in Ruby?
An array containing two elements
2, 3
Error
Random pair of elements from the array
15.
In Ruby, what is the purpose of the `reduce` method on an array?
Returns the sum of all elements in the array
Sorts the elements in a collection in ascending order
Combines all elements of the array into a single value based on a binary operation
Filters out elements in a collection based on a condition
16.
What is the purpose of the `lambda` keyword in Ruby?
Creates a new object
Defines a class
Defines a method
Defines an anonymous function
17.
What does the `!` (bang) in a method name often indicate in Ruby?
A safe or non-destructive version of the method
A dangerous or destructive version of the method
The end of the method
A block for the method
18.
In Ruby, what is the purpose of the `freeze` method on an object?
Allows the object to be modified
Prevents the object from being modified
Doubles the size of the object
Resets the object to its default state
19.
In Ruby, what is the purpose of the `fetch` method on a hash?
Retrieves the value for a given key or a default value if the key is not found
Deletes a key-value pair based on a condition
Returns the sum of all values in the hash
Sorts the hash by key
20.
In Ruby, how do you check if a file exists?
File.exist?('filename')
file_exists?('filename')
check_file_existence('filename')
filename.exists?