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 is the correct syntax for creating a hash in Ruby?

  1. A. {"key1": "value1", "key2": "value2"}
  2. B. hash = {}
  3. C. hash.add("key", "value")
  4. D. Hash.new("key", "value")
Report Error

What is the correct way to define a conditional statement in Ruby?

  1. A. if (condition) { code }
  2. B. if condition then code end
  3. C. if condition: code
  4. D. when condition; code
Report Error

Which of these operators is used for string concatenation?

  1. A. +
  2. B. *
  3. C. -
  4. D. /
Report Error

How do you add two numbers in Ruby?

  1. A. add(a
  2. B. B. a + b
  3. C. a.add(b)
  4. D. b.add(a)
Report Error

What is the correct syntax for printing a message to the console?

  1. A. console.log("message")
  2. B. print("message")
  3. C. puts("message")
  4. D. write("message")
Report Error

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

  1. A. var name = "value"
  2. B. let name = "value"
  3. C. name = "value"
  4. D. const name = "value"
Report Error

Which method is used to convert a string to uppercase in Ruby?

  1. A. .to_uppercase
  2. B. .upper
  3. C. .upcase
  4. D. .uppercase
Report Error

What is the default file extension for Ruby files?

  1. A. .ru
  2. B. .rby
  3. C. .rb
  4. D. .ruby
Report Error

Which of the following is the correct way to define a constant in Ruby?

  1. A. CONSTANT_NAME = value
  2. B. constant_name = value
  3. C. CONSTANT_NAME : value
  4. D. constant name = value
Report Error

In Ruby, what is the keyword used to define a class?

  1. A. class
  2. B. define
  3. C. cls
  4. D. defclass
Report Error

Which of the following is the correct way to start a single-line comment in Ruby?

  1. A. //
  2. B. /*
  3. C. #
  4. D. --
Report Error

What does the 'print' method do in Ruby?

  1. A. Prints to console
  2. B. Converts to string
  3. C. Reads input from user
  4. D. Finds the length
Report Error