General Ruby IV
1.
What is the result of `3.times.map { |n| n* 2 }` in Ruby?
0, 2, 4
2, 4, 6
0, 1, 2
1, 3, 5
2.
How do you check if a variable is defined in Ruby?
defined?(variable)
variable.defined?
is_defined(variable)
check_definition(variable)
3.
What is the purpose of the `prepend` method on a string in Ruby?
Counts the number of characters in the string
Appends a specified string to the end of another string
Reverses the characters of the string
Prepends a specified string to the beginning of another string
4.
In Ruby, what does the `nil?` method do?
Checks if an object is `nil`
Checks if a variable is `nil`
Checks if a method is `nil`
Creates a `nil` object
5.
What is the purpose of the `include?` method in Ruby?
Checks if an object is a symbol
Checks if an object is included in an array
Checks if an object is frozen
Checks if an object is empty
6.
What is the purpose of the `+=` operator in Ruby?
Decrements a variable by a specified value
Increments a variable by a specified value
Multiplies a variable by a specified value
Divides a variable by a specified value
7.
In Ruby, how do you open and write to a file?
write_file(filename)
File.open(filename, 'w')
open_file(filename, 'w')
File.create(filename, 'w')
8.
What does the `each_char` method do on a string in Ruby?
Counts the number of characters in the string
Iterates over each word of the string
Reverses the characters of the string
Iterates over each character of the string
9.
In Ruby, what is the purpose of the `include?` method on a string?
Counts the occurrences of a substring in the string
Checks if the string is empty
Checks if a substring is present in the string
Reverses the characters of the string
10.
What is the result of `[1, 2, 3].reduce(0) { |sum, n| sum + n }` in Ruby?
6
3
0
1
11.
What is the purpose of the `private` keyword in Ruby?
Defines methods that can only be accessed from the same module
Defines methods that can be accessed from anywhere
Defines methods that can only be accessed within the class hierarchy
Defines methods that can only be accessed within the same class
12.
In Ruby, what method is used to open a file for reading?
File.open('file.txt', 'r')
File.create('file.txt', 'read')
File.read('file.txt')
File.load('file.txt')
13.
How do you create a new instance of a class in Ruby?
create_instance
New.instance
Class.new
Instance.create
14.
What is the primary purpose of the `attr_reader` method in Ruby?
Creates a class-level attribute
Creates a write-only attribute for an instance variable
Creates a read-only attribute for an instance variable
Creates a getter method for a class variable
15.
In Ruby, how do you define a private method within a class?
def my_method
private
private_method
private def
16.
In Ruby, what is the purpose of the `!` (bang) in a method name?
Signals the end of a method
Indicates a safe or non-destructive version of a method
Indicates a dangerous or destructive version of a method
Specifies a block for a method
17.
In Ruby, what does the `freeze` method do on an object?
Resets the object to its default state
Allows the object to be modified
Doubles the size of the object
Prevents the object from being modified
18.
How do you convert a symbol to a string in Ruby?
symbol.str
str(symbol)
symbol.to_s
convert(symbol)
19.
Which method is used to remove whitespace from both ends of a string in Ruby?
clean
trim
remove_whitespace
strip
20.
In Ruby, which method is used to merge two hashes into a new hash?
combine
merge
append
unite