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)
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()
In Ruby, what symbol is used to denote the start of a comment?
a) //
b) #
c) <!--
d) /*
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
How is an array defined in Ruby?
a) []
b) array[]
c) {}
d) new array
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
Which keyword is used to define a block in Ruby?
a) function
b) block
c) do
d) yield
How do you exit a loop prematurely in Ruby?
a) end
b) stop
c) exit
d) break
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)
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
Which type of comment is used for single-line comments in Ruby?
a) #
b) //
c) /*
d) <!--
How do you write a multi-line comment in Ruby?
a) =begin and =end
b) /* */
c) //
d) { }
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
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
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)
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
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
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
In Ruby, how is a global variable defined?
a) $variable_name
b) @variable_name
c) @@variable_name
d) variable_name$
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
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
What is the symbol used to denote an instance variable in Ruby?
a) @
b) $
c) @@
d) #
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)
Which of the following is a valid number data type in Ruby?
a) Integer
b) Float
c) Rational
d) All of the above
What is the result of the expression 5 / 2 in Ruby?
a) 2.5
b) 2
c) 3
d) Error
What is the Ruby string interpolation syntax?
a) #{}
b) <<
c) ()
d) []
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
Which of the following is a symbol in Ruby?
a) "symbol"
b) :symbol
c) symbol()
d) symbol[]
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
What method is used to convert a number into a string in Ruby?
a) to_string()
b) str()
c) to_s()
d) stringify()
How would you check if a variable is an integer in Ruby?