General Ruby I
1.
Which method is used to check if a key exists in a hash in Ruby?
key_exists?
exists?
include?
key?
2.
How do you comment a single line in Ruby?
# // /* */
--
/* */
##
3.
What does the `each` method do on an array in Ruby?
Adds an element to the array
Iterates over each element
Removes an element from the array
Sorts the array
4.
What is the purpose of the `super` keyword in Ruby?
Calls the method of the same name in the parent class
Stops the execution of the program
Skips the current iteration of a loop
Creates a new instance of a class
5.
How do you open and read a file in Ruby?
File.read(filename)
read_file(filename)
open_file(filename)
read(filename)
6.
What is the result of `5 + 3 * 2` in Ruby?
16
20
13
11
7.
Which keyword is used to define a class in Ruby?
class_def
define
class
create
8.
In Ruby, what is a block?
A section of code enclosed in `do...end`
A data type
An error message
A method call
9.
What is the output of `puts "hello" * 3` in Ruby?
hellohellohello
3hello
hello3
error
10.
What does the `puts` method do in Ruby?
Performs mathematical operations
Defines a variable
Prints to the console
Creates a loop
11.
Which method is used to remove the last element from an array in Ruby?
remove
delete
last
pop
12.
What is the purpose of the `attr_accessor` in a Ruby class?
Creates getter and setter methods for instance variables
Defines a class variable
Inherits from another class
Initializes an object
13.
How do you check if a variable is `nil` in Ruby?
variable.nil?
nil?(variable)
is_nil(variable)
check_nil(variable)
14.
Which method is used to convert a string to an integer in Ruby?
to_i
parse_int
int
string_to_i
15.
What is the default scope of a local variable in Ruby?
Global
Local
Instance
Class
16.
In Ruby, how do you define a method?
def method_name
method method_name
define method_name
function method_name
17.
How is a symbol different from a string in Ruby?
Symbols are immutable and unique
Strings are immutable and unique
Symbols can have spaces
Strings are mutable
18.
In Ruby, what is the purpose of the `require` keyword?
Defines a method
Declares a variable
Includes external libraries or files into the program
Imports a module
19.
What is the result of `true && false` in Ruby?
error
false
true
nil
20.
What is the result of `puts [1, 2, 3].sample` in Ruby?
Random element from the array
1, 2, or 3
Error
An array containing one element