General Ruby III
1.
How do you define a constant in Ruby?
By using all lowercase letters
By using all uppercase letters
By using a combination of uppercase and lowercase letters
By using symbols
2.
How do you create a new instance of a class in Ruby?
Class.new
new Class()
create Class
Class.create
3.
What is the output of `"hello".length` in Ruby?
6
5
4
1
4.
What is the result of `true || false` in Ruby?
nil
FALSE
error
TRUE
5.
In Ruby, what is the purpose of the `collect` method on an array?
Sorts the array in ascending order
Removes the last element from an array
Applies a block to each element and returns a new array with the results
Combines all elements into a single value
6.
In Ruby, how do you check if a string contains a specific substring?
string.includes?("substring")
substring.in?(string)
string.contains?("substring")
string.include?("substring")
7.
What is the result of `3 * 4 / 2` in Ruby?
3
6
12
2
8.
What is the purpose of the `protected` keyword in Ruby?
Defines private methods
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
9.
What does the `select` method do on an array in Ruby?
Sorts the array in ascending order
Returns a new array with elements that satisfy a condition
Removes the first element from the array
Reverses the order of the array
10.
Which method is used to convert a symbol to a string in Ruby?
to_str
convert_str
str
to_s
11.
In Ruby, what is the purpose of the `attr_accessor`?
Inherits from another class
Defines a class variable
Creates getter and setter methods for instance variables
Initializes an object
12.
What is the output of `puts [1, 2, 3].map {|n| n * 2 }` in Ruby?
1, 4, 2009
1, 2, 2003
3, 6, 2009
2, 4, 2006
13.
How do you remove a key-value pair from a hash in Ruby?
hash.remove(key)
hash.delete(key)
hash.pop(key)
hash.exclude(key)
14.
What is the purpose of the `next` keyword in a Ruby loop?
Throws an exception
Exits the loop prematurely
Restarts the loop
Skips the current iteration and moves to the next one
15.
In Ruby, what is the purpose of the `module_function` method?
Defines a new module
Makes all methods of a module accessible as module functions
Excludes methods from a module
Makes all methods of a module private
16.
How do you round a floating-point number to the nearest integer in Ruby?
number.floor
number.ceil
number.round
round(number)
17.
How do you check if a key exists in a hash in Ruby?
key_exists?
exists?
include?
key?
18.
In Ruby, what does the `chomp` method do on a string?
Removes trailing whitespace from a string
Removes leading whitespace from a string
Removes specified characters from the end of a string
Converts a string to uppercase
19.
What is the purpose of the `retry` keyword in a Ruby rescue block?
Restarts the current iteration of a loop
Exits the program
Jumps to the end of the program
Raises a custom exception
20.
In Ruby, what is the purpose of the `include` module?
Excludes methods from a module
Imports a module into another module
Includes methods from a module into a class
Defines a module variable