Commerce

Basic 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?

  1. A. Removes trailing whitespace
  2. B. Converts to integer
  3. C. Reads input from user
  4. D. Splits a string
Report Error

Which symbol is used for the modulo operation in Ruby?

  1. A. %
  2. B. *
  3. C. /
  4. D. &
Report Error

What is the correct way to declare and initialize a variable in Ruby?

  1. A. var_name = value
  2. B. var var_name = value
  3. C. variable var_name = value
  4. D. variable_name = value
Report Error

What is the correct way to cast a string to an integer in Ruby?

  1. A. string.to_i
  2. B. string.integer
  3. C. int(string)
  4. D. string.convert(:integer)
Report Error

How do you get the current date and time in Ruby?

  1. A. date = Date.now
  2. B. time = Time.now
  3. C. datetime = DateTime.now
  4. D. now = Date.today
Report Error

What is the purpose of the require keyword in Ruby?

  1. A. Loads a specific Ruby file
  2. B. Defines a new module
  3. C. Creates a new class
  4. D. Imports external libraries
Report Error

How do you pass arguments to a method in Ruby?

  1. A. method_name(arg1, arg2)
  2. B. method_name { arg1, arg2 }
  3. C. method_name.with(arg1, arg2)
  4. D. method_name arg1 arg2
Report Error

What is the correct way to define a block in Ruby?

  1. A. block { code }
  2. B. do code end
  3. C. function code
  4. D. block code
Report Error

How do you compare two objects in Ruby?

  1. A. object1 == object2
  2. B. object1.equals(object2)
  3. C. object1 === object2
  4. D. compare(object1, object2)
Report Error

What is the correct syntax for string interpolation in Ruby?

  1. A. "#{variable}"
  2. B. $(variable)
  3. C. %(variable)
  4. D. variable.to_string
Report Error

How do you define a range in Ruby?

  1. A. range = "start..end"
  2. B. range = start..end
  3. C. range = Range.new("start", "end")
  4. D. range = start.to(end)
Report Error

What is the correct syntax for accessing a class method?

  1. A. Class.method_name
  2. B. object.method_name
  3. C. Class.new.method_name
  4. D. method_name(Class)
Report Error