Ruby Online Quiz Test

Welcome to our Ruby Online Quiz Test! This test consists of 25 multiple-choice questions designed to gauge your understanding of Ruby, a dynamic, open-source programming language focused on simplicity and productivity. 

Whether you are starting your programming journey or are an experienced Ruby developer, this quiz will help you test your knowledge, identify strengths, and discover areas for further learning. Ready to dive into the elegant and expressive world of Ruby? Let’s begin your test!

1. What does the following Ruby code return?

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

2. How do you create a new hash in Ruby?

a) new Hash{}
b) Hash.new
c) {}
d) Both b) and c) are correct

3. Which method is used to remove nil values from an array in Ruby?

a) strip
b) compact
c) clean
d) squeeze

4. What is the output of this Ruby code?

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

5. How do you define a method in Ruby that calculates the square of a number?

a) def square(number) number * number end
b) function square(number) { return number * number; }
c) square = -> (number) { number * number }
d) All of the above

6. What does the : symbol represent in Ruby?

a) A string
b) A method call
c) A symbol
d) A variable assignment

7. How do you add elements to an array in Ruby?

a) arr << 1; arr.push(2)
b) arr.add(1, 2)
c) arr += [1, 2]
d) Both a) and c) are correct

8. What is the purpose of the yield keyword in Ruby?

a) To pause the execution of a method
b) To exit from a loop
c) To pass control from a method to the block given to it
d) To generate random numbers

9. Which is the correct way to declare a class in Ruby?

a) class Dog def bark puts "Woof!" end end
b) class Dog { def bark() { puts "Woof!" } }
c) Dog = Class.new do def bark puts "Woof!" end end
d) Both a) and c) are correct

10. What will this Ruby code output?

x = "hello"
puts x.object_id == "hello".object_id
a) true
b) false
c) nil
d) Error

11. How can you convert the string "123" to the integer 123 in Ruby?

a) "123".to_i
b) Integer("123")
c) "123".convert(:integer)
d) Both a) and b) are correct

12. Which method can be used to execute a block of code repeatedly for a fixed number of times?

a) loop
b) for
c) repeat
d) times

13. What is a module in Ruby?

a) A way to manage Ruby gems
b) A group of methods and constants that can be mixed into classes
c) A type of Ruby class
d) A package management system

14. How do you check if a key exists in a hash?

a) h.has_key?("apple")
b) h.exists?("apple")
c) h.include?("apple")
d) h.key?("apple")

15. What does the @@ symbol represent in Ruby?

a) A global variable
b) An instance variable
c) A class variable
d) A constant

16. How do you open a file for reading in Ruby?

a) File.open("example.txt", "r")
b) File.read("example.txt")
c) File.get("example.txt")
d) Open.file("example.txt")

17. What is the output of this Ruby code?

a = "Hello"
a.freeze
a << " world"
a) "Hello world"
b) "Hello"
c) RuntimeError
d) nil

18. How do you create a symbol in Ruby?

a) :symbol
b) #symbol
c) &symbol
d) $symbol

19. What is the self keyword used for in Ruby?

a) To exit a method early
b) To refer to the current instance of the class
c) To define a method as private
d) To call a method asynchronously

20. How do you handle exceptions in Ruby?

a) begin-rescue-end
b) try-catch-finally
c) error-handling
d) check-try

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

a) "hello".each_char { |c| puts c }
b) "hello".each { |c| puts c }
c) "hello".chars { |c| puts c }
d) "hello".split("").each { |c| puts c }

22. How can you convert an array to a string in Ruby?

a) join
b) to_s
c) stringify
d) Both a) and b) are correct

23. What does include? method do in Ruby?

a) Checks if a script is included
b) Checks if a value is included in an array
c) Includes a module
d) None of the above

24. What will the following Ruby code output?

puts "ruby" == "ruby"
a) true
b) false
c) nil
d) Error

25. How do you declare a method that takes a variable number of arguments?

a) def print_all(*args)
b) def print_all(args[])
c) def print_all(**args)
d) def print_all(args...)

Comments