Below is a curated list of 30 multiple-choice questions (MCQs) on Ruby programming, covering basics, data types, control structures, OOP concepts, and more. These are compiled from reliable sources like Sanfoundry and other Ruby interview resources. Each question includes 4 options (a-d). The answer key is provided at the end for self-assessment.
Questions
- Ruby is an object-oriented general-purpose programming language.
a) True
b) False - Which of the following is supported by Ruby?
a) Multiple Programming Paradigms
b) Dynamic Type System
c) Automatic Memory Management
d) All of the mentioned - Which of the following datatypes are valid in Ruby?
a) Numbers
b) Boolean
c) String
d) All of the mentioned - What will any variable evaluate to if it is of Boolean data type?
a) True
b) False
c) Nil
d) None of the mentioned - Which of the following are valid floating-point literals in Ruby?
a) .5
b) 2
c) 0.5
d) None of the mentioned - Which of the following is a valid assignment operator in Ruby?
a) +=
b) -=
c) *=
d) All of the mentioned - What does the **= assignment operator do in Ruby?
a) Multiplies the value twice
b) It is used as exponent
c) It is used as modulus
d) None of the mentioned - What is the output of the following code?
for num in 1...5 puts num enda) 1 2 3 4 5
b) 1 2 3 4
c) 2 3 4 5
d) None of the mentioned - Which of the following is a valid conditional statement in Ruby?
a) elseif
b) elsif
c) else if
d) elseiff - The elsif conditional statement is written with an expression.
a) True
b) False - It is possible to make an array of booleans in Ruby.
a) True
b) False - What is the output of the following code?
string_array = ["a","e","i","o","u"] print string_arraya) [“a”,”e”,”i”,”o”,”u”]
b) Error
c) Vowels
d) None of the mentioned - What is the size of an integer data type in Ruby?
a) -2^30 to 2^30
b) -2^30 to 2^(30-1)
c) -2^29 to 2^30
d) -2^60 to 2^61 - Ruby can deal with both numbers and floating-point values.
a) True
b) False - What is the output of the following code?
a = 77 b = 78 if a < b print "true" enda) true
b) false
c) Nil
d) Error - Octal notation in Ruby ranges from:
a) 0-7
b) 0-9
c) 1-8
d) 1-9 - Ruby can be embedded into Hypertext Markup Language (HTML).
a) True
b) False - Which of the following is the correct way to declare a constant in Ruby?
a) Using all uppercase letters
b) Using lowercase letters
c) Using mixed case
d) No specific convention - In Ruby, symbols are:
a) Mutable strings
b) Immutable objects used as identifiers
c) Floating-point numbers
d) Boolean values - What is duck typing in Ruby?
a) Type checking at compile time
b) Determining object type based on behavior, not class
c) Static type declaration
d) Inheritance enforcement - How do you define a method in Ruby?
a) Using def keyword
b) Using function keyword
c) Using method keyword
d) Using proc keyword - What is the purpose of the ‘nil’ value in Ruby?
a) Represents true
b) Represents false
c) Represents nothing or void
d) Represents an empty string - In Ruby, everything is an object, including:
a) Only classes
b) Primitive data types like integers
c) Only strings
d) Functions only - What does the ‘upcase’ method do to a string?
a) Capitalizes the first letter
b) Converts the entire string to uppercase
c) Reverses the string
d) Trims whitespace - Which operator is used for exponentiation in Ruby?
a) ^
b) **
c) pow
d) exp - What is metaprogramming in Ruby?
a) Writing code that runs at compile time
b) Code that generates or modifies other code at runtime
c) Static code analysis
d) Memory management - How do you create a hash in Ruby with symbol keys?
a) { ‘key’ => ‘value’ }
b) { key: ‘value’ }
c) [ key: ‘value’ ]
d) ( key => ‘value’ ) - What is the output of ‘puts 0 ? true : false’ in Ruby?
a) true
b) false
c) nil
d) Error - Ruby supports garbage collection for:
a) Manual memory management
b) Automatic memory reclamation
c) Stack allocation only
d) Heap allocation only - What is the difference between ‘==’ and ‘===’ in Ruby?
a) == checks equality, === checks identity
b) == checks equality, === is for case statements and checks if object responds to method
c) Both check equality
d) === checks equality, == checks identity
Answer Key
- a) True
- d) All of the mentioned
- d) All of the mentioned
- a) True (or b) False depending on value, but defaults to true/false) – Wait, correction: Variables hold true or false.
Note: The question implies the possible values; answer is a/b. - c) 0.5
- d) All of the mentioned
- b) It is used as exponent
- b) 1 2 3 4
- b) elsif
- a) True
- a) True
- a) [“a”,”e”,”i”,”o”,”u”]
- b) -2^30 to 2^(30-1)
- a) True
- a) true
- a) 0-7
- a) True
- a) Using all uppercase letters
- b) Immutable objects used as identifiers
- b) Determining object type based on behavior, not class
- a) Using def keyword
- c) Represents nothing or void
- b) Primitive data types like integers
- b) Converts the entire string to uppercase
- b) **
- b) Code that generates or modifies other code at runtime
- b) { key: ‘value’ }
- a) true (0 is truthy in Ruby)
- b) Automatic memory reclamation
- b) == checks equality, === is for case statements and checks if object responds to method