Ruby Online Quiz

Welcome to our Ruby online quiz, crafted to challenge and refine your Ruby programming skills. Ruby is a dynamic, open-source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. This quiz encompasses a wide range of questions, from basic to advanced topics, aimed at both beginners and experienced Rubyists. Dive into these questions to test your knowledge and perhaps discover something new about Ruby!

1. What will the following Ruby code output?

puts "Ruby Quiz".length
a) 8
b) 9
c) 10
d) 11

2. In Ruby, which method is used to convert a string into an integer?

a) to_i
b) to_int
c) parse_int
d) convert

3. What does the following Ruby code snippet return?

x = 10
y = 20
puts x + y
a) 10
b) 20
c) 30
d) "1020"

4. Which of the following is a valid Ruby hash declaration?

a) hash = { key: "value" }
b) hash = { "key" => "value" }
c) Both a) and b)
d) None of the above

5. What is the output of the following Ruby code?

def add(a, b)
return a + b
end
puts add(2, 3)
a) 2
b) 3
c) 5
d) Error

6. What does the nil? method do in Ruby?

a) Checks if the variable is defined
b) Checks if the variable is true
c) Checks if the variable is false
d) Checks if the variable is nil

7. What will the following Ruby code output?

a = [1, 2, 3]
puts a[2]
a) 1
b) 2
c) 3
d) nil

8. In Ruby, which keyword is used to define a class?

a) class
b) def
c) function
d) method

9. How do you write a comment in Ruby?

a) /* comment */
b) // comment
c) # comment
d) ' comment

10. What is the output of the following Ruby code?

if "ruby".include?("y")
puts "Yes"
else
puts "No"
end
a) Yes
b) No
c) True
d) False

11. Which method can you use to remove an element from the end of an array in Ruby?

a) pop
b) push
c) delete
d) remove

12. What does the : symbol represent in Ruby?

a) A string
b) A variable
c) A symbol
d) An error

13. What will the following Ruby code output?

x = "Hello "
y = "World"
puts x.concat(y)
a) Hello
b) World
c) HelloWorld
d) Hello World

14. Which looping structure is used to iterate over each element in an array in Ruby?

a) for
b) each
c) loop
d) while

15. What is the output of the following Ruby code?

puts "abc".upcase
a) abc
b) ABC
c) error
d) ABCD

16. How do you create a new instance of a class in Ruby?

a) Class.new()
b) new Class()
c) Class.create()
d) Class()

17. What is the output of the following Ruby code?

numbers = [1, 2, 3, 4, 5]
numbers.delete_if { |number| number < 3 }
puts numbers
a) [1, 2]
b) [3, 4, 5]
c) [1, 2, 3, 4, 5]
d) []

18. What does the && operator do in Ruby?

a) It performs an OR operation
b) It performs an AND operation
c) It concatenates strings
d) It compares two values

19. How do you declare a global variable in Ruby?

a) Using the @ symbol
b) Using the @@ symbol
c) Using the $ symbol
d) Using the & symbol

20. What is the output of the following Ruby code?

lambda = -> { return "Hello, Ruby!" }
puts lambda.call
a) Hello, Ruby!
b) Hello, Ruby
c) Error
d) nil

21. In Ruby, which method is used to reverse a string?

a) reverse
b) flip
c) invert
d) back

22. What does the gsub method do in Ruby?

a) Globally substitutes a substring with another substring
b) Finds all occurrences of a pattern in a string
c) Replaces the first occurrence of a substring
d) Splits a string into an array

23. What will the following Ruby code output?

str = "hello"
str[0] = "H"
puts str
a) hello
b) Hello
c) Error
d) H

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

a) join
b) to_s
c) merge
d) connect

25. What is the output of the following Ruby code?

puts 1.odd?
a) true
b) false
c) error
d) none of the above

Comments