MCQs on Ruby Syntax and Basics | Ruby

Explore essential Ruby syntax rules, data types, variables, constants, and the importance of comments in this guide. Understand how to write clean, effective Ruby code while leveraging its powerful features.


MCQs on Ruby Syntax and Basics

Section 1: Ruby Syntax Rules (8 Questions)

  1. Which of the following is the correct syntax for defining a method in Ruby?
    • a) def method_name()
    • b) method_name() = def
    • c) method method_name()
    • d) function method_name()
  2. In Ruby, what symbol is used to denote the start of a comment?
    • a) //
    • b) #
    • c) <!--
    • d) /*
  3. What is the correct syntax for creating a class in Ruby?
    • a) class ClassName {}
    • b) class ClassName do end
    • c) class ClassName()
    • d) class ClassName() do end
  4. How is an array defined in Ruby?
    • a) []
    • b) array[]
    • c) {}
    • d) new array
  5. What is the correct way to concatenate strings in Ruby?
    • a) string1 + string2
    • b) string1.concat(string2)
    • c) string1 . join string2
    • d) Both a and b
  6. Which keyword is used to define a block in Ruby?
    • a) function
    • b) block
    • c) do
    • d) yield
  7. How do you exit a loop prematurely in Ruby?
    • a) end
    • b) stop
    • c) exit
    • d) break
  8. In Ruby, what will the statement 5.times { print "Hello" } output?
    • a) “Hello” five times
    • b) “5 times Hello”
    • c) Error
    • d) “Hello” printed once

Section 2: Comments and Documentation (6 Questions)

  1. What is the purpose of comments in Ruby?
    • a) To speed up the code execution
    • b) To explain and document the code
    • c) To define variables
    • d) To create loops
  2. Which type of comment is used for single-line comments in Ruby?
    • a) #
    • b) //
    • c) /*
    • d) <!--
  3. How do you write a multi-line comment in Ruby?
    • a) =begin and =end
    • b) /* */
    • c) //
    • d) { }
  4. What is the correct syntax for adding a documentation comment for a method in Ruby?
    • a) # This is a comment
    • b) /* This is a comment */
    • c) =begin This is a comment =end
    • d) # Method description
  5. Which of the following is not a valid comment in Ruby?
    • a) # A single-line comment
    • b) /* Multi-line comment */
    • c) // Invalid comment
    • d) =begin Multi-line comment =end
  6. What is the purpose of the YARD tool in Ruby?
    • a) To optimize the code
    • b) To compile Ruby code
    • c) To generate documentation from comments
    • d) To run Ruby code in a virtual environment

Section 3: Variables and Constants (8 Questions)

  1. Which of the following is the correct syntax to declare a variable in Ruby?
    • a) variable_name = value
    • b) value = variable_name
    • c) variable_name : value
    • d) let variable_name = value
  2. What is the naming convention for a local variable in Ruby?
    • a) Starts with a capital letter
    • b) Starts with a lowercase letter
    • c) Can contain spaces
    • d) Starts with an underscore
  3. Which of the following correctly defines a constant in Ruby?
    • a) const PI = 3.14
    • b) Constant PI = 3.14
    • c) PI = 3.14
    • d) constant PI = 3.14
  4. In Ruby, how is a global variable defined?
    • a) $variable_name
    • b) @variable_name
    • c) @@variable_name
    • d) variable_name$
  5. What is the scope of a class variable in Ruby?
    • a) Accessible across all classes
    • b) Accessible only within the class
    • c) Accessible across all objects of a class
    • d) Not used in Ruby
  6. What is the behavior of a constant in Ruby if you attempt to change its value?
    • a) It throws an error
    • b) It updates the value without any issue
    • c) It triggers a warning
    • d) It reverts to its original value
  7. What is the symbol used to denote an instance variable in Ruby?
    • a) @
    • b) $
    • c) @@
    • d) #
  8. What happens when you reference a variable that has not been initialized in Ruby?
    • a) It throws an error
    • b) It returns nil
    • c) It returns a warning
    • d) It is automatically initialized to false

Section 4: Data Types (8 Questions)

  1. Which of the following is a valid number data type in Ruby?
    • a) Integer
    • b) Float
    • c) Rational
    • d) All of the above
  2. What is the result of the expression 5 / 2 in Ruby?
    • a) 2.5
    • b) 2
    • c) 3
    • d) Error
  3. What is the Ruby string interpolation syntax?
    • a) #{}
    • b) <<
    • c) ()
    • d) []
  4. Which of the following is used to represent booleans in Ruby?
    • a) true and false
    • b) 1 and 0
    • c) True and False
    • d) Yes and No
  5. Which of the following is a symbol in Ruby?
    • a) "symbol"
    • b) :symbol
    • c) symbol()
    • d) symbol[]
  6. What is the difference between a string and a symbol in Ruby?
    • a) Strings are mutable, symbols are immutable
    • b) Symbols are mutable, strings are immutable
    • c) Strings are objects, symbols are primitives
    • d) There is no difference
  7. What method is used to convert a number into a string in Ruby?
    • a) to_string()
    • b) str()
    • c) to_s()
    • d) stringify()
  8. How would you check if a variable is an integer in Ruby?
    • a) variable.is_integer?
    • b) variable.kind_of?(Integer)
    • c) variable.is_a?(Integer)
    • d) Both b and c

Answer Key

QnoAnswer (Option with the text)
1a) def method_name()
2b) #
3b) class ClassName do end
4a) []
5d) Both a and b
6c) do
7d) break
8a) “Hello” five times
9b) To explain and document the code
10a) #
11a) =begin and =end
12d) # Method description
13c) // Invalid comment
14c) To generate documentation from comments
15a) variable_name = value
16b) Starts with a lowercase letter
17a) const PI = 3.14
18a) $variable_name
19c) Accessible across all objects of a class
20c) It triggers a warning
21a) @
22b) It returns nil
23d) All of the above
24b) 2
25a) #{}
26a) true and false
27b) :symbol
28a) Strings are mutable, symbols are immutable
29c) to_s()
30d) Both b and c

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top