Debug School

Akanksha
Akanksha

Posted on

Top 30 Ruby Interview Questions with Answers

1. What is Ruby?

A) A precious gem
B) A programming language
C) A popular web browser
D) A type of coffee
Answer: B) A programming language

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

A) my_variable
B) 123_variable
C) _variable
D) variable123
Answer: B) 123_variable

3. What is the result of 5 + "3".to_i in Ruby?

A) 8
B) "53"
C) Error
D) 53
Answer: A) 8

4. In Ruby, how do you define a constant variable?

A) Using let
B) Using const
C) Using define
D) Using CAPITAL_LETTERS
Answer: D) Using CAPITAL_LETTERS

5. Which Ruby method is used to join elements of an array into a single string?

A) combine
B) join
C) concat
D) merge
Answer: B) join

6. What does the attr_accessor method do in a Ruby class?

A) Defines a class variable
B) Generates getter and setter methods for instance variables
C) Adds an instance variable
D) Creates a constructor for the class
Answer: B) Generates getter and setter methods for instance variables

7. What is the purpose of the super keyword in Ruby?

A) To call the parent class constructor
B) To exit a loop
C) To define a superclass
D) To define a subclass
Answer: A) To call the parent class constructor

8. What does the yield keyword do in Ruby?

A) Terminates the program
B) Raises an exception
C) Calls a block of code
D) Defines a class
Answer: C) Calls a block of code

9. Which method is used to check if a Ruby object is nil?

A) null?
B) nil?
C) empty?
D) none?
Answer: B) nil?

10. What does the self keyword refer to in Ruby?

A) The current class
B) The superclass
C) The current object
D) The global scope
Answer: C) The current object

11. How do you define a class method in Ruby?

A) By prefixing the method name with a dot
B) By prefixing the method name with a dollar sign ($)
C) By prefixing the method name with a double underscore (__)
D) By prefixing the method name with a self.
Answer: D) By prefixing the method name with a self.

12. Which operator is used for concatenating strings in Ruby?

A) +
B) -
C) *
D) /
Answer: A) +

13. What is the purpose of the BEGIN block in Ruby?

A) It defines a class
B) It executes code before the program starts
C) It defines a method
D) It raises an exception
Answer: B) It executes code before the program starts

14. How do you create a comment in Ruby?

A) Using //
B) Using /* /
**C) Using #
*
D) Using --
Answer: C) Using #

15. 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) 5 5 5 5 5
D) Error
Answer: A) 0 1 2 3 4

16. Which data type is used to represent a range of values in Ruby?

A) Range
B) Interval
C) Set
D) Array
Answer: A) Range

17. What is the purpose of the unless keyword in Ruby?

A) To define a loop
B) To check if a condition is true
C) To execute code if a condition is false
D) To define a class
Answer: C) To execute code if a condition is false

18. How do you import external libraries or gems in Ruby?

A) Using the include keyword
B) Using the require keyword
C) Using the import keyword
D) Using the use keyword
Answer: B) Using the require keyword

19. Which Ruby method is used to remove the last element from an array and return it?

A) pop
B) shift
C) unshift
D) push
Answer: A) pop

20. What does the to_s method do in Ruby?

A) Converts a string to an integer
B) Converts an object to a string
C) Converts an integer to a float
D) Converts a string to a symbol
Answer: B) Converts an object to a string

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

A) Using the create keyword
B) Using the new keyword
C) Using the instance keyword
D) Using the instantiate keyword
Answer: B) Using the new keyword

22. Which data structure in Ruby allows you to store key-value pairs?

A) Array
B) Hash
C) Set
D) Queue
Answer: B) Hash

23. What is the purpose of the break keyword in a loop in Ruby?

A) To start a loop
B) To skip an iteration
C) To exit the loop
D) To continue to the next iteration
Answer: C) To exit the loop

24. What is the output of puts "Hello, #{name}" when name is undefined?

A) "Hello, #{name}"
B) "Hello, "
C) Error
D) "Hello, undefined"
Answer: B) "Hello, "

25. Which method is used to check if a value exists in an array in Ruby?

A) exists?
B) find
C) include?
D) search
Answer: C) include?

26. What is the result of "hello".upcase in Ruby?

A) "hello"
B) "HELLO"
C) "Hello"
D) Error
Answer: B) "HELLO"

27. In Ruby, what is the result of "hello" * 3?

A) "hellohellohello"
B) "hello3"
C) Error
D) "hellohellohellohello"
Answer: A) "hellohellohello"

28. How do you round a floating-point number to the nearest integer in Ruby?

A) round
B) ceil
C) floor
D) int
Answer: A) round

29. What does the += operator do in Ruby?

A) Adds the right operand to the left operand
B) Subtracts the right operand from the left operand
C) Multiplies the left operand by the right operand
D) Concatenates the left operand with the right operand
Answer: A) Adds the right operand to the left operand

30. How do you open a file in read mode in Ruby?

A) File.open("file.txt", "r")
B) File.read("file.txt")
C) File.open("file.txt", "w")
D) File.write("file.txt")
Answer: A) File.open("file.txt", "r")

Top comments (0)