General Ruby X
1.
How do you define a method with a variable number of parameters in Ruby?
def method_name(*args)
def method_name(params...)
def method_name(parameters)
def method_name(param1, param2 = default_value)
2.
What is the output of `"ruby".chars.shuffle.join` in Ruby?
Random permutation of the characters in the string
ruby
r
Error
3.
How do you define a class in Ruby?
define class ClassName
class ClassName
create class ClassName
new Class(ClassName)
4.
What is the result of `puts 3.times.map { |n| n*2 }' in Ruby?
0, 2, 4
2, 4, 6
0, 1, 2
1, 3, 5
5.
What does the `include?` method do on an array in Ruby?
Counts the number of elements in the array
Checks if a specified element is present in the array
Sorts the array in ascending order
Reverses the order of the array
6.
What is the result of `puts [1, 2, 3, 4].reduce(0) { | sum, n| sum + n}' in Ruby?
1
0
10
Error
7.
In Ruby, what is the purpose of the `freeze` method on an object?
Prevents the object from being modified
Allows the object to be modified
Doubles the size of the object
Resets the object to its default state
8.
What is the result of `puts [1, 2, 3, 4].shuffle.first` in Ruby?
1
Random element from the array
4
Error
9.
What is the purpose of the `reject` method on an array in Ruby?
Returns the first element of the array
Sorts the array in ascending order
Reverses the order of the array
Returns a new array with elements that do not satisfy a condition
10.
In Ruby, what is the purpose of the `module_function` method?
Excludes methods from a module
Defines a new module
Makes all methods of a module accessible as module functions
Makes all methods of a module private
11.
In Ruby, what is the purpose of the `collect` method on an array?
Returns the first element of the array
Sorts the array in ascending order
Reverses the order of the array
Returns a new array with the results of applying a block to each element
12.
What is the purpose of the `times` method on an integer in Ruby?
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 does the `+=` operator do in Ruby?
Increments the variable by a specified value
Decrements the variable by a specified value
Adds a value to the variable
Subtracts a value from the variable
14.
In Ruby, what is the purpose of the `yield` keyword in a method?
Passes control to a block associated with the method
Returns a value from the method
Raises an exception
Declares a local variable
15.
How do you define a symbol in Ruby?
symbol_name
:symbol_name
Symbol.symbol_name
create_symbol(symbol_name)
16.
In Ruby, what is the purpose of the `class` keyword?
Defines a class
Defines a method
Defines a module
Creates an instance of a class
17.
What is the purpose of the `gsub` method on a string in Ruby?
Returns the index of the first occurrence of a pattern
Replaces all occurrences of a pattern with a specified string
Counts the occurrences of a pattern in the string
Returns a substring that matches a pattern
18.
How do you check if a variable is `nil` in Ruby?
nil?(variable)
is_nil(variable)
check_nil(variable)
variable.nil?
19.
In Ruby, what does the `<<` operator do on a string?
Appends another string to the end of the original string
Prepends another string to the beginning of the original string
Concatenates two strings
Removes the last character from the string
20.
In Ruby, what is the purpose of the `compact` method on an array?
Removes `nil` elements from the array
Returns a new array with duplicate elements removed
Sorts the array in ascending order
Reverses the order of the array