Top 50 Ruby Interview Questions with Answers

Ruby Interview Questions with Answers

1. What is Ruby?

a) A programming language
b) A web browser
c) A database management system
d) An operating system

Answer: a) A programming language

2. Which of the following is not a valid variable name in Ruby?

a) my_variable
b) my-variable
c) MyVariable
d) myVariable

Answer: b) my-variable

3. What is the difference between an instance variable and a class variable in Ruby?

a) An instance variable is shared by all instances of a class, while a class variable is unique to each instance.
b) An instance variable is unique to each instance of a class, while a class variable is shared by all instances of a class.
c) An instance variable is not used in Ruby programming, while a class variable is used to store class-level data.
d) There is no difference between an instance variable and a class variable in Ruby.

Answer: b) An instance variable is unique to each instance of a class, while a class variable is shared by all instances of a class.

4. What does the attr_accessor method do in Ruby?

a) Defines a new attribute for a class
b) Allows read and write access to an instance variable
c) Allows read-only access to an instance variable
d) Allows write-only access to an instance variable

Answer: b) Allows read and write access to an instance variable

5. In Ruby, what is the difference between ‘and’ and ‘&&’?

a) There is no difference
b) ‘and’ has higher precedence than ‘&&’
c) ‘&&’ has higher precedence than ‘and’
d) ‘and’ is used for logical OR operations, while ‘&&’ is used for logical AND operations

Answer: c) ‘&&’ has higher precedence than ‘and’

6. Which of the following is not a valid loop construct in Ruby?

a) for
b) while
c) until
d) if

Answer: d) if

7. What is the difference between puts and print in Ruby?

a) There is no difference
b) puts adds a line break after the output, while print does not
c) print adds a line break after the output, while puts does not
d) puts and print are not valid Ruby commands

Answer: b) puts adds a line break after the output, while print does not

8. Which of the following is not a valid method for an array in Ruby?

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

Answer: c) remove

9. What is the result of 5.times { |i| puts i } in Ruby?

a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 2 4 6 8
d) Nothing is outputted

Answer: a) 0 1 2 3 4

11. What is the purpose of a block in Ruby?

a) To define a new class
b) To define a new method
c) To group statements and pass them to a method
d) To create a new object

Answer: c) To group statements and pass them to a method

12. What is the result of “hello”.reverse in Ruby?

a) “hello”
b) “loleh”
c) “olleh”
d) “e”

Answer: c) “olleh”

12. What does the following code do in Ruby?
array = [1,2,3]
array.map! { |x| x * 2 }

a) It creates a new array with each element multiplied by 2
b) It creates a new array with each element added by 2
c) It modifies the current array by multiplying each element by 2
d) It modifies the current array by adding 2 to each element

Answer: c) It modifies the current array by multiplying each element by 2

13. What does the ||= operator do in Ruby?

a) It assigns a value only if the current value is nil or false
b) It assigns a value regardless of the current value
c) It checks if two variables are equal
d) It performs a bit-wise OR operation

Answer: a) It assigns a value only if the current value is nil or false

14. What is the result of (1..5).to_a in Ruby?

a) [0, 1, 2, 3, 4, 5]
b) [1, 2, 3, 4, 5]
c) [1, 3, 5]
d) [0, 1, 2, 3, 4]

Answer: b) [1, 2, 3, 4, 5]

15. Which of the following is not a valid way to create a hash in Ruby?

a) { key1: value1, key2: value2 }
b) { “key1” => “value1”, “key2” => “value2” }
c) Hash.new(key: value)
d) { :key1 => “value1”, :key2 => “value2” }

Answer: c) Hash.new(key: value)

16. What is a module in Ruby?

a) A way to define a new class
b) A way to group related methods and constants
c) A way to define a new variable
d) A way to define a new block

Answer: b) A way to group related methods and constants

17. What is the result of [1,2,3].inject(:+) in Ruby?

a) 6
b) [1,2,3]
c) [1, 3, 6]
d) Nothing is outputted

Answer: a) 6

18. What is the purpose of the ‘self’ keyword in Ruby?

a) To define a new class
b) To define a new module
c) To refer to the current object
d) To refer to the parent object

Answer: c) To refer to the current object

19. What is the difference between ‘raise’ and ‘rescue’ in Ruby?

a) There is no difference
b) ‘raise’ is used to raise an exception, while ‘rescue’ is used to handle an exception
c) ‘rescue’ is used to raise an exception, while ‘raise’ is used to handle an exception
d) ‘raise’ and ‘rescue’ are not valid Ruby commands

Answer: b) ‘raise’ is used to raise an exception, while ‘rescue’ is used to handle an exception

20. What is a gem in Ruby?

a) A way to define a new class
b) A way to group related methods and constants
c) A way to define a new method
d) A way to package and distribute Ruby code

Answer: d) A way to package and distribute Ruby code

21. What is the result of 10.divmod(3) in Ruby?

a) 3
b) 1
c) [3, 1]
d) [1, 3]

Answer: c) [3, 1]

22. What is the difference between a lambda and a proc in Ruby?

a) There is no difference
b) A lambda checks the number of arguments passed, while a proc does not
c) A proc checks the number of arguments passed, while a lambda does not
d) A lambda can be called with or without arguments, while a proc must be called with arguments

Answer: b) A lambda checks the number of arguments passed, while a proc does not

23. What is the result of “hello”.gsub(‘l’, ‘b’) in Ruby?

a) “hello”
b) “hb”
c) “hebbo”
d) “helbo”

Answer: d) “helbo”

24. Which of the following is not a valid way to define a class in Ruby?

a) class MyClass; end
b) MyClass = class; end
c) class MyClass < ParentClass; end
d) class MyClass; def my_method; end; end

Answer: b) MyClass = class; end

25. What is the result of “hello world”.split in Ruby?

a) [“hello”, “world”]
b) “hello world”
c) “helloworld”
d) [“h”, “e”, “l”, “l”, “o”, ” “, “w”, “o”, “r”, “l”, “d”]

Answer: a) [“hello”, “world”]

26. What is the purpose of the return keyword in Ruby?

a) To define a new variable
b) To define a new method
c) To stop the execution of a method and return a value
d) To refer to the current object

Answer: c) To stop the execution of a method and return a value

27. What is the result of {‘a’ => 1, ‘b’ => 2}.invert in Ruby?

a) {‘1’ => ‘a’, ‘2’ => ‘b’}
b) {‘a’ => ‘1’, ‘b’ => ‘2’}
c) {1 => ‘a’, 2 => ‘b’}
d) {‘a’ => 2, ‘b’ => 1}

Answer: {1 => ‘a’, 2 => ‘b’}

28. What is the difference between ‘puts’ and ‘p’ in Ruby?

a) There is no difference
b) ‘p’ adds a line break after the output, while ‘puts’ does not
c) ‘puts’ adds a line break after the output, while ‘p’ does not
d) ‘puts’ prints the object and its type, while ‘p’ only prints the object

Answer: d) ‘puts’ prints the object and its type, while ‘p’ only prints the object

29. What is the result of [1,2,3].each { |x| puts x * 2 } in Ruby?

a) [2, 4, 6]
b) Nothing is outputted
c) 2 4 6
d) 1 2 3

Answer: b) Nothing is outputted

30. What is the result of ‘hello’.length in Ruby?

a) 4
b) 5
c) 6
d) 7

Answer: b) 5

31. Which of the following is not a valid way to create a range in Ruby?

a) 1..5
b) 1…5
c) (1, 5)
d) Range.new(1, 5)

Answer: c) (1, 5)

32. What is the purpose of the ‘unless’ keyword in Ruby?

a) To define a new variable
b) To define a new loop
c) To define a new method
d) To execute code only if a condition is false

Answer: d) To execute code only if a condition is false

33. What is the result of “hello”.upcase in Ruby?

a) “hello”
b) “HELLO”
c) “Hello”
d) “hello world”

Answer: b) “HELLO”

34. What is the difference between ‘include’ and ‘extend’ in Ruby?

a) There is no difference
b) ‘include’ adds methods as instance methods, while ‘extend’ adds methods as class methods
c) ‘extend’ adds methods as instance methods, while ‘include’ adds methods as class methods
d) ‘include’ and ‘extend’ are not valid Ruby commands

Answer: b) ‘include’ adds methods as instance methods, while ‘extend’ adds methods as class methods

35. What is the result of [1,2,3].reduce(:+) in Ruby?

a) 6
b) [1,2,3]
c) [1, 3, 6]
d) Nothing is outputted

Answer: a) 6

36. Which of the following is not a valid way to access a hash value in Ruby?

a) hash[key]
b) hash.fetch(key)
c) hash.get(key)
d) hash.values_at(key)

Answer: c) hash.get(key)

37. What is the result of “hello”.index(‘l’) in Ruby?

a) 0
b) 1
c) 2
d) 3

Answer: b) 1

38. What does the following code do in Ruby?

class MyClass
def initializer
puts “Hello, world!”
end
end

a) Defines a new method
b) Defines a new class
c) Prints “Hello, world!” to the console
d) There is a typo in the code and it will not run

Answer: b) Defines a new class

39. What is the purpose of the ‘nil’ keyword in Ruby?

a) To define a new variable
b) To indicate that a value is unknown or undefined
c) To stop the execution of a method and return a value
d) To refer to the current object

Answer: b) To indicate that a value is unknown or undefined

40. What is the result of ‘hello’.include?(‘lo’) in Ruby?

a) true
b) false
c) nil
d) There is a syntax error in the code

Answer: a) true

41. What is the difference between ‘next’ and ‘break’ in Ruby?

a) There is no difference
b) ‘next’ skips the current iteration of a loop, while ‘break’ stops the loop entirely
c) ‘break’ skips the current iteration of a loop, while ‘next’ stops the loop entirely
d) ‘next’ and ‘break’ are not valid Ruby commands

Answer: b) ‘next’ skips the current iteration of a loop, while ‘break’ stops the loop entirely

42. What is the result of “hello world”.capitalize in Ruby?

a) “hello world”
b) “Hello world”
c) “Hello World”
d) “hELLO WORLD”

Answer: b) “Hello world”

43. What is the purpose of the ‘super’ keyword in Ruby?

a) To refer to the current object
b) To call the parent method with the same name
c) To define a new class
d) To define a new module

Answer: b) To call the parent method with the same name

44. What is the result of “hello”.concat(” world”) in Ruby?

a) “hello world”
b) “helloworld”
c) “worldhello”
d) “hell worldo”

Answer: a) “hello world”

45. What is the difference between == and === in Ruby?

a) There is no difference
b) == is used for exact matches, while === is used for range comparisons
c) === is used for exact matches, while == is used for pattern matching
d) === is not a valid Ruby operator

Answer: c) === is used for exact matches, while == is used for pattern matching

46. What is the result of [1,2,3].select { |x| x % 2 == 0 } in Ruby?

a) [1]
b) [2]
c) [3]
d) [2,3]

Answer: b) [2]

47. What is the result of “hello”.delete(‘lo’) in Ruby?

a) “hello”
b) “heo”
c) “ho”
d) “el”

Answer: d) “el”

48. What is the result of (1..5).reduce(:*) in Ruby?

a) 120
b) [1,2,3,4,5]
c) [1,5,10,15,20,25]
d) Nothing is outputted

Answer: a) 120

49. What is the purpose of the ‘eval’ method in Ruby?

a) To define a new variable
b) To define a new method
c) To execute a string as Ruby code
d) To execute a file as Ruby code

Answer: c) To execute a string as Ruby code

50. What is the difference between ‘private’ and ‘protected’ in Ruby?

a) There is no difference
b) ‘private’ restricts access to a method to the current object, while ‘protected’ restricts access to a method to the current object and its subclasses
c) ‘protected’ restricts access to a method to the current object, while ‘private’ restricts access to a method to the current object and its subclasses
d) ‘private’ and ‘protected’ are not valid Ruby commands

Answer: b) ‘private’ restricts access to a method to the current object, while ‘protected’ restricts access to a method to the current object and its subclasses

Ashwani Kumar
Latest posts by Ashwani Kumar (see all)
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x