21
What is the correct syntax for creating a hash in Ruby?
A)
{"key1": "value1", "key2": "value2"}
B)
hash = {}
C)
hash.add("key", "value")
D)
Hash.new("key", "value")
Answer & Solution
Answer: Option
A
22
How do you access a value from a hash in Ruby?
A)
hash.get("key")
B)
hash["key"]
C)
hash.at("key")
D)
hash.find("key")
Answer & Solution
Answer: Option
B
23
What is the correct way to define a method in Ruby?
A)
def method_name(parameters) code end
B)
function method_name(parameters) code
C)
method method_name(parameters) = code
D)
method_name(parameters) -> code
Answer & Solution
Answer: Option
A
24
How do you call a method in Ruby?
A)
method_name()
B)
object.method_name
C)
object.method_name()
D)
method_name(object)
Answer & Solution
Answer: Option
B
25
What are the different types of comments in Ruby?
A)
Single-line // and multi-line /* */
B)
Single-line # and multi-line ``
C)
Single-line # and multi-line """ """
D)
Single-line // and multi-line """ """
Answer & Solution
Answer: Option
C
26
What is the difference between =~ and == operators?
A)
=~ checks for string pattern matching, == checks for equality
B)
=~ checks for equality, == checks for string pattern matching
C)
Both operators are the same
D)
None of the above
Answer & Solution
Answer: Option
A
27
What is the difference between nil and false?
A)
nil represents absence of value, false represents a boolean value
B)
Both are the same
C)
nil is a string, false is a boolean
D)
nil is a number, false is a boolean
Answer & Solution
Answer: Option
A
28
What is the correct way to define a symbol in Ruby?
A)
symbol = "value"
B)
symbol = :value
C)
symbol = 'value'
D)
symbol = Symbol("value")
Answer & Solution
Answer: Option
B
29
What is the purpose of the gem command in Ruby?
A)
Installs and manages Ruby libraries
B)
Compiles Ruby code
C)
Runs Ruby code
D)
Defines Ruby classes
Answer & Solution
Answer: Option
A
30
What is the correct way to define an array in Ruby?
A)
array = ["element1", "element2"]
B)
array = Array.new
C)
array = new Array
D)
array = list("element1", "element2")
Answer & Solution
Answer: Option
A