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 does the 'chomp' method do in Ruby?
- A. Removes trailing whitespace
- B. Converts to integer
- C. Reads input from user
- D. Splits a string
Correct Answer: A
Which symbol is used for the modulo operation in Ruby?
- A. %
- B. *
- C. /
- D. &
Correct Answer: A
What is the correct way to declare and initialize a variable in Ruby?
- A. var_name = value
- B. var var_name = value
- C. variable var_name = value
- D. variable_name = value
Correct Answer: D
What is the correct way to cast a string to an integer in Ruby?
- A. string.to_i
- B. string.integer
- C. int(string)
- D. string.convert(:integer)
Correct Answer: A
How do you get the current date and time in Ruby?
- A. date = Date.now
- B. time = Time.now
- C. datetime = DateTime.now
- D. now = Date.today
Correct Answer: B
What is the purpose of the require keyword in Ruby?
- A. Loads a specific Ruby file
- B. Defines a new module
- C. Creates a new class
- D. Imports external libraries
Correct Answer: D
How do you pass arguments to a method in Ruby?
- A. method_name(arg1, arg2)
- B. method_name { arg1, arg2 }
- C. method_name.with(arg1, arg2)
- D. method_name arg1 arg2
Correct Answer: A
What is the correct way to define a block in Ruby?
- A. block { code }
- B. do code end
- C. function code
- D. block code
Correct Answer: A
How do you compare two objects in Ruby?
- A. object1 == object2
- B. object1.equals(object2)
- C. object1 === object2
- D. compare(object1, object2)
Correct Answer: A
What is the correct syntax for string interpolation in Ruby?
- A. "#{variable}"
- B. $(variable)
- C. %(variable)
- D. variable.to_string
Correct Answer: A
How do you define a range in Ruby?
- A. range = "start..end"
- B. range = start..end
- C. range = Range.new("start", "end")
- D. range = start.to(end)
Correct Answer: B
What is the correct syntax for accessing a class method?
- A. Class.method_name
- B. object.method_name
- C. Class.new.method_name
- D. method_name(Class)
Correct Answer: A