CommerceBasic Syntax In Ruby MCQs
Practice Basic Syntax In Ruby MCQs for competitive exams.
Basic Syntax In Ruby MCQs
Practice questions from this topic.
What is the default return value of a method in Ruby?
- A. nil
- B. TRUE
- C. FALSE
- D. The value of the last expression
Correct Answer: A
How do you break out of a loop in Ruby?
- A. exit()
- B. break()
- C. continue()
- D. end()
Correct Answer: B
How do you check the truthiness of a value in Ruby?
- A. if (value) then code end
- B. if value do code end
- C. if value != false
- D. if value.is_truthy?
Correct Answer: C
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")
Correct Answer: A
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
Correct Answer: A
What is the correct way to define a symbol in Ruby?
- A. symbol = "value"
- B. symbol = :value
- C. symbol = 'value'
- D. symbol = Symbol("value")
Correct Answer: B
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
Correct Answer: A
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
Correct Answer: A
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 """ """
Correct Answer: C
How do you call a method in Ruby?
- A. method_name()
- B. object.method_name
- C. object.method_name()
- D. method_name(object)
Correct Answer: B
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
Correct Answer: A
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")
Correct Answer: B