General Ruby VI
1.
How do you check if a number is even in Ruby?
check_even(number)
is_even(number)
number.even?
even?(number)
2.
What is the purpose of the `case` statement in Ruby?
Creates an array
Defines a class
Defines a method
A conditional structure for multiple branches
3.
What is the result of `puts 2**3` in Ruby?
2
6
8
Error
4.
What is the output of `"hello".include?("ll")` in Ruby?
FALSE
TRUE
h
Error
5.
What is the purpose of the `lambda` keyword in Ruby?
Defines an anonymous function
Defines a class
Defines a method
Creates a new object
6.
What is the output of `puts [1, 2, 3].shuffle` in Ruby?
1, 2, 2003
Random order of elements
3, 2, 2001
Error
7.
How do you convert a string to a symbol in Ruby?
symbolize(string)
sym(string)
convert_to_sym(string)
string.to_sym
8.
In Ruby, how do you check if a variable is an instance of a specific class?
check_instance(variable, Class)
instance?(variable, Class)
is_instance(variable, Class)
variable.instance_of?(Class)
9.
In Ruby, what is the purpose of the `times` method on an integer?
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
10.
What is the result of `puts (1..5).to_a` in Ruby?
1, 2, 3, 4, 5
An array containing numbers from 1 to 5
5, 4, 3, 2, 1
Error
11.
In Ruby, what is the purpose of the `flatten` 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 that is a one-dimensional flattening of the original array
12.
How do you define a hash in Ruby?
new Hash()
hash.new
hash = {}
create_hash{}
13.
What is the purpose of the `break` keyword in a Ruby loop?
Exits the loop prematurely
Continues to the next iteration of the loop
Prints a message to the console
Throws an exception
14.
In Ruby, what is the purpose of the `elsif` keyword in a conditional statement?
Ends the conditional statement
Introduces an additional condition to check
Prints a message to the console
Defines a new variable
15.
How do you round a floating-point number to the nearest integer in Ruby?
number.round
number.floor
number.ceil
round(number)
16.
What is the purpose of the `map` method in Ruby?
Applies a block to each element and returns a new array with the results
Removes the last element from an array
Sorts the array in ascending order
Combines all elements into a single value
17.
In Ruby, what is the purpose of the `attr_accessor`?
Defines a class variable
Creates getter and setter methods for instance variables
Inherits from another class
Initializes an object
18.
What does the `compact` method do on an array in Ruby?
Removes `nil` elements from the array
Sorts the array in ascending order
Reverses the order of the array
Returns the first element of the array
19.
In Ruby, what is the purpose of the `gsub` method on a string?
Returns a substring that matches a pattern
Returns the index of the first occurrence of a pattern
Counts the occurrences of a pattern in the string
Replaces all occurrences of a pattern with a specified string
20.
Which method is used to remove the last element from an array in Ruby?
last
delete
pop
remove