Ruby Quiz - MCQ Questions and Answers

Dive into our Ruby Quiz! This quiz features 25 multiple-choice questions that will test your knowledge of Ruby, a dynamic programming language known for its simplicity and productivity. The quiz covers a wide range of Ruby topics, from basic syntax and built-in methods to more advanced topics like blocks, mixins, and metaprogramming. 

Whether you are just starting with Ruby or an experienced developer looking to test your skills, this quiz is a great way to assess your understanding and improve your grasp of Ruby programming. Let’s see how much you know about Ruby—start the quiz and challenge yourself!

1. What does the puts method do in Ruby?

a) Prints the input to the console, adding a newline at the end.
b) Returns the input.
c) Saves the input to a file.
d) Sends the input to a database.

2. How do you create an array in Ruby?

a) Array.new(1, 2, 3)
b) [1, 2, 3]
c) array = 1, 2, 3
d) Array{1, 2, 3}

3. What will the following Ruby code output?

x = "Hello"
puts x.downcase
a) HELLO
b) Hello
c) hello
d) No output

4. What is a block in Ruby?

a) A module where data is temporarily stored.
b) A section of code that is always executed first.
c) A collection of code that can take arguments and return values.
d) An error handling mechanism.

5. How do you define a method in Ruby that takes an argument?

def greet(name)
"Hello, #{name}!"
end
a) By using the def keyword and parentheses.
b) By using the method keyword and square brackets.
c) By using the func keyword and curly braces.
d) By using the define keyword and no punctuation.

6. What is the purpose of the @ symbol in Ruby?

a) It indicates a class variable.
b) It indicates a global variable.
c) It indicates an instance variable.
d) It starts a new line in strings.

7. What does the following Ruby code return?

[1, 2, 3].map { |n| n * 2 }
a) [2, 4, 6]
b) [1, 2, 3]
c) nil
d) Error

8. Which method can be used to remove a key-value pair from a hash by key in Ruby?

a) delete
b) remove
c) discard
d) erase

9. What will the following Ruby code output?

puts "racecar".reverse
a) racecar
b) raccecar
c) rraaccee
d) No output

10. How do you access the last element of an array in Ruby?

arr = [1, 2, 3, 4, 5]
puts arr.last
a) 5
b) 1
c) 4
d) 3

11. What is Ruby on Rails?

a) A Ruby compiler.
b) A web development framework written in Ruby.
c) A package manager for Ruby.
d) A Ruby database engine.

12. What does the include keyword do in Ruby?

a) Imports methods from one module into another.
b) Includes a file in another file.
c) Adds properties to a class.
d) All of the above.

13. What will the following Ruby code output?

puts "hello".capitalize
a) Hello
b) hello
c) HELLO
d) No output

14. How do you iterate over each character in a string in Ruby?

"hello".each_char { |c| print c, ' ' }
a) h e l l o
b) hello
c) h e l l o_
d) Error

15. What is a symbol in Ruby?

a) A string that cannot be modified.
b) A method for embedding operations.
c) A numeric value.
d) A type of Ruby variable.

16. What will the following Ruby code output?

a = [1, 2, 3]
b = a
b.pop
puts a
a) [1, 2]
b) [1, 2, 3]
c) [1]
d) Error

17. How do you generate a random number between 0 and 10 in Ruby?

a) rand(10)
b) random(0..10)
c) rand(0..10)
d) random(10)

18. What will the following Ruby code return?

[:foo, :bar, :baz].index(:bar)
a) 1
b) 2
c) :bar
d) None of the above

19. What is the purpose of yield in Ruby?

a) To pause the execution of the program.
b) To call a block passed to a method.
c) To return control to the operating system.
d) To terminate a loop.

20. How do you convert a string to an integer in Ruby?

"42".to_i
a) 42
b) "42"
c) Error
d) 0

21. What is the purpose of the self keyword in Ruby?

a) To terminate a program.
b) To refer to the current instance of the class.
c) To define a class method.
d) To create a new instance of a class.

22. What does the gsub method do in Ruby?

"hello".gsub('l', 'r')
a) herro
b) hello
c) helo
d) error

23. How do you check if an array contains a specific element in Ruby?

a) [].contains?
b) [].include?
c) [].has_key?
d) [].exists?

24. What will the following Ruby code output?

puts 1 == 1.0
a) true
b) false
c) Error
d) nil

25. How do you declare a hash with default values in Ruby?

Hash.new(0)
a) {}
b) {0}
c) Error
d) A hash where default values are 0

Comments