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 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")
Correct Answer: A
What is the correct way to define a conditional statement in Ruby?
- A. if (condition) { code }
- B. if condition then code end
- C. if condition: code
- D. when condition; code
Correct Answer: A
Which of these operators is used for string concatenation?
- A. +
- B. *
- C. -
- D. /
Correct Answer: A
How do you add two numbers in Ruby?
- A. add(a
- B. B. a + b
- C. a.add(b)
- D. b.add(a)
Correct Answer: B
What is the correct syntax for printing a message to the console?
- A. console.log("message")
- B. print("message")
- C. puts("message")
- D. write("message")
Correct Answer: C
What is the correct way to declare a variable in Ruby?
- A. var name = "value"
- B. let name = "value"
- C. name = "value"
- D. const name = "value"
Correct Answer: C
Which method is used to convert a string to uppercase in Ruby?
- A. .to_uppercase
- B. .upper
- C. .upcase
- D. .uppercase
Correct Answer: C
What is the default file extension for Ruby files?
- A. .ru
- B. .rby
- C. .rb
- D. .ruby
Correct Answer: C
Which of the following is the correct way to define a constant in Ruby?
- A. CONSTANT_NAME = value
- B. constant_name = value
- C. CONSTANT_NAME : value
- D. constant name = value
Correct Answer: A
In Ruby, what is the keyword used to define a class?
- A. class
- B. define
- C. cls
- D. defclass
Correct Answer: A
Which of the following is the correct way to start a single-line comment in Ruby?
- A. //
- B. /*
- C. #
- D. --
Correct Answer: C
What does the 'print' method do in Ruby?
- A. Prints to console
- B. Converts to string
- C. Reads input from user
- D. Finds the length
Correct Answer: A