General Ruby II
1.
What is the purpose of the `unless` keyword in Ruby?
Ends the program
Executes code if a condition is true
Executes code if a condition is false
Skips the current iteration of a loop
2.
What is the result of `"hello".reverse` in Ruby?
lo
h
olleh
hello
3.
How do you convert a number to a string in Ruby?
number.to_s
str(number)
convert(number)
stringify(number)
4.
How do you round a floating-point number to the nearest integer in Ruby?
number.floor
number.round
number.ceil
round(number)
5.
What is the purpose of the `break` keyword in a Ruby loop?
Continues to the next iteration of the loop
Exits the loop prematurely
Prints a message to the console
Throws an exception
6.
What is the purpose of the `attr_reader` in Ruby?
Defines a setter method for an instance variable
Defines a getter method for an instance variable
Creates a new instance of a class
Inherits from another class
7.
In Ruby, what is the purpose of the `self` keyword?
Refers to the child class
Refers to the parent class
Refers to the current instance of the class
Refers to the current class
8.
What does the `elsif` keyword do in a Ruby `if` statement?
Ends the conditional statement
Introduces an additional condition to check
Prints a message to the console
Defines a new variable
9.
In Ruby, what is the purpose of the `yield` keyword in a method?
Declares a local variable
Returns a value from the method
Raises an exception
Passes control to a block associated with the method
10.
In Ruby, what does the `respond_to?` method do?
Checks if an object responds to a method
Checks if a variable is defined
Checks if a method is defined
Checks if a class is defined
11.
How do you raise a custom exception in Ruby?
raise ExceptionType, "Message"
throw "Message"
throw ExceptionType, "Message"
raise "Message"
12.
In Ruby, what is the purpose of the `module` keyword?
Defines a namespace for methods and constants
Defines a class
Defines a method
Defines a variable
13.
What is the purpose of the `attr_writer` in Ruby?
Defines a getter method for an instance variable
Defines a setter method for an instance variable
Creates a new instance of a class
Inherits from another class
14.
In Ruby, what does the `rand` method do?
Generates a random string
Generates a random integer
Generates a random boolean
Generates a random float between 0 and 1
15.
What is the purpose of the `__FILE__` constant in Ruby?
Returns the directory of the current file
Returns the name of the current file
Returns the line number of the current file
Returns the size of the current file
16.
In Ruby, what is the purpose of the `reduce` method on an array?
Filters out elements in a collection based on a condition
Sorts the elements in a collection in ascending order
Returns the sum of all elements in the array
Combines all elements of the array into a single value based on a binary operation
17.
What is the result of `puts 'hello'.upcase` in Ruby?
h
Hello
HELLO
hello
18.
How do you define a constant in Ruby?
By using all uppercase letters
By using all lowercase letters
By using a combination of uppercase and lowercase letters
By using symbols
19.
Which method is used to add elements to the end of an array in Ruby?
push
append
add
insert
20.
What does the `each_with_index` method do on an array in Ruby?
Iterates over every other element
Iterates over each element in reverse order
Iterates over each element with its index
Iterates over each element twice