Ruby is a dynamic, object-oriented programming language known for its simplicity, flexibility, and productivity. It was created in the mid-1990s by Yukihiro Matsumoto (often referred to as “Matz”) in Japan and was first released to the public in 1995. Ruby is influenced by several programming languages, including Perl, Smalltalk, Eiffel, and Lisp, and it is designed to prioritize programmer happiness and productivity.
Key Features of Ruby:
Object-Oriented: Ruby is a fully object-oriented language, meaning everything in Ruby is an object, including data types and functions.
Dynamic Typing: Ruby is dynamically typed, which allows variables to automatically change their data type as needed during runtime.
Simple and Readable Syntax: Ruby has a clean and easy-to-read syntax that emphasizes human readability and writing code that is close to natural language.
Garbage Collection: Ruby has automatic memory management through garbage collection, which relieves developers from managing memory manually.
Duck Typing: Ruby uses duck typing, which means the type or class of an object is determined by its behavior rather than its explicit type.
Blocks and Closures: Ruby supports blocks and closures, allowing developers to pass blocks of code as arguments to methods.
Mixins: Ruby allows the inclusion of modules (mixins) in classes, enabling code reuse and providing a flexible way to add functionalities to multiple classes.
Reflection and Metaprogramming: Ruby supports reflection, allowing developers to examine and modify a program’s structure and behavior during runtime.
You might like to know
Create an auto-grading quiz/assessment without any coding – try OnlineExamMaker today!
Table of content
- Part 1: 30 Ruby quiz questions & answers
- Part 2: Download Ruby questions & answers for free
- Part 3: Free online quiz platform – OnlineExamMaker
Part 1: 30 Ruby quiz questions & answers
1. What type of programming language is Ruby?
a) Procedural
b) Object-oriented
c) Functional
d) Scripting
Answer: b) Object-oriented
2. Who is the creator of Ruby?
a) Guido van Rossum
b) Linus Torvalds
c) Yukihiro Matsumoto
d) Larry Wall
Answer: c) Yukihiro Matsumoto
3. Which of the following is the correct way to define a class in Ruby?
a) class MyClass
b) class = MyClass
c) MyClass class
d) MyClass()
Answer: a) class MyClass
4. In Ruby, what is the symbol used to denote instance variables?
a) $
b) @
c) #
d) %
Answer: b) @
5. Which Ruby data type is used to represent a sequence of characters?
a) Integer
b) Float
c) String
d) Array
Answer: c) String
6. What does IRB stand for in Ruby?
a) Interactive Ruby
b) Integrated Ruby Building
c) Integrated Rails Bundle
d) Interactive Ruby Console
Answer: a) Interactive Ruby
7. What is the output of the following Ruby code snippet?
“`
x = 5
y = 10
z = x + y
puts z
“`
a) 5
b) 10
c) 15
d) Error
Answer: c) 15
8. In Ruby, how do you define a method?
a) func method_name {}
b) def method_name
c) method_name { }
d) define method_name
Answer: b) def method_name
9. Which method is used to convert a string to uppercase in Ruby?
a) upcase
b) upper
c) uppercase
d) to_upper
Answer: a) upcase
10. What is the output of the following Ruby code snippet?
“`
array = [1, 2, 3, 4, 5]
array.each do |num|
puts num * 2
end
“`
a) 1, 2, 3, 4, 5
b) 2, 4, 6, 8, 10
c) 2, 3, 4, 5, 6
d) 1, 4, 9, 16, 25
Answer: b) 2, 4, 6, 8, 10
11. What does the “self” keyword refer to in a Ruby class?
a) The parent class
b) The current object instance
c) The class itself
d) The superclass
Answer: b) The current object instance
12. In Ruby, what does the “attr_accessor” method do?
a) Defines a new method in a class
b) Provides read and write accessors for an instance variable
c) Retrieves the class of an object
d) Creates a new instance of a class
Answer: b) Provides read and write accessors for an instance variable
13. What is the correct syntax for inheriting a class in Ruby?
a) class ChildClass < BaseClass
b) class ChildClass : BaseClass
c) class ChildClass extends BaseClass
d) class ChildClass > BaseClass
Answer: a) class ChildClass < BaseClass
14. What is the purpose of the "require" keyword in Ruby?
a) To include a module in a class
b) To define a new class
c) To include external libraries or files
d) To create a loop
Answer: c) To include external libraries or files
15. What is the output of the following Ruby code snippet?
```
10.times do |i|
puts i if i.even?
end
```
a) 0, 2, 4, 6, 8, 10
b) 1, 3, 5, 7, 9
c) 1, 2, 3, 4, 5
d) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Answer: a) 0, 2, 4, 6, 8
Part 2: Download Ruby questions & answers for free
Download questions & answers for free
16. In Ruby, how do you add a new element to an array?
a) array.add(element)
b) array.insert(element)
c) array.push(element)
d) array.append(element)
Answer: c) array.push(element)
17. What is the output of the following Ruby code snippet?
“`
age = 25
if age >= 18
puts “You are an adult.”
else
puts “You are a minor.”
end
“`
a) You are an adult.
b) You are a minor.
c) adult
d) minor
Answer: a) You are an adult.
18. What is the purpose of the “each_with_index” method in Ruby?
a) To iterate over an array with the element and its index
b) To convert an array to a hash
c) To filter elements in an array
d) To delete elements from an array
Answer: a) To iterate over an array with the element and its index
19. What is the output of the following Ruby code snippet?
“`
x = 10
unless x > 20
puts “x is not greater than 20.”
else
puts “x is greater than 20.”
end
“`
a) x is not greater than 20.
b) x is greater than 20.
c) x is 10.
d) x is greater than or equal to 20.
Answer: a) x is not greater than 20.
20. In Ruby, what does the “&&” operator represent?
a) Logical OR
b) Logical NOT
c) Logical AND
d) Bitwise AND
Answer: c) Logical AND
21. What is the purpose of the “times” method in Ruby?
a) To multiply two numbers
b) To iterate a block of code a specific number of times
c) To divide two numbers
d) To calculate the remainder of a division
Answer: b) To iterate a block of code a specific number of times
22. What is the output of the following Ruby code snippet?
“`
x = 5
y = 3
puts x % y
“`
a) 1
b) 2
c) 3
d) 5
Answer: a) 1
23. Which method is used to remove the last element from an array in Ruby?
a) remove_last
b) pop
c) delete_last
d) shift
Answer: b) pop
24. In Ruby, how do you define a range of numbers from 1 to 10?
a) 1..10
b) 1, 10
c) [1, 10]
d) range(1, 10)
Answer: a) 1..10
25. What is the output of the following Ruby code snippet?
“`
fruits = [“apple”, “orange”, “banana”]
puts fruits[1]
“`
a) apple
b) orange
c) banana
d) [“apple”, “orange”, “banana”]
Answer: b) orange
26. What is the purpose of the “map” method in Ruby?
a) To create a new array with transformed elements
b) To iterate over a hash
c) To filter elements in an array
d) To delete elements from an array
Answer: a) To create a new array with transformed elements
Pro Tip
Want to assess your learners online? Create an online quiz for free!
27. Which Ruby method is used to check if a key exists in a hash?
a) contains_key?
b) key_exists?
c) has_key?
d) includes_key?
Answer: c) has_key?
28. In Ruby, what does the “times” method return?
a) The sum of the elements in the array
b) The product of the elements in the array
c) The index of the element in the array
d) The number of elements in the array
Answer: d) The number of elements in the array
29. What is the output of the following Ruby code snippet?
“`
def greet(name)
puts “Hello, #{name}!”
end
greet(“Alice”)
greet(“Bob”)
“`
a) Hello, Alice!
Hello, Bob!
b) Hello, Bob!
Hello, Alice!
c) Hello, name!
Hello, name!
d) Error
Answer: a) Hello, Alice!
Hello, Bob!
30. Which Ruby method is used to concatenate two arrays?
a) add
b) append
c) concatenate
d) +
Answer: d) +
Part 3: Best online quiz making platform – OnlineExamMaker
With OnlineExamMaker quiz maker, teachers can easily create, customize, and distribute quizzes with just a few clicks. The intuitive interface provides a range of question types, including multiple-choice, true or false, fill in the blank, and open-ended questions, enabling users to make professional assessments that suit their specific needs.
Create Your Next Quiz/Exam with OnlineExamMaker