Ruby MCQ

Ruby is a dynamic, open-source programming language known for its simplicity and productivity. Dive into the set of 30 multiple-choice questions to test your knowledge on Ruby programming language.

1. What is the creator of Ruby's name?

a) Guido van Rossum
b) Brendan Eich
c) Yukihiro Matsumoto
d) Linus Torvalds

Answer:

c) Yukihiro Matsumoto

Explanation:

Yukihiro Matsumoto, also known as "Matz", is the creator of Ruby.

2. Which method checks if an array is empty?

a) isEmpty
b) nil?
c) empty?
d) none?

Answer:

c) empty?

Explanation:

The empty? method is used to check if an array is empty in Ruby.

3. Which of the following is used to define an instance variable in Ruby?

a) $variableName
b) @@variableName
c) variableName
d) @variableName

Answer:

d) @variableName

Explanation:

Instance variables in Ruby are prefixed with an @ symbol. This denotes that the variable belongs to an instance of the class.

4. What does the following Ruby code return? ["Ruby", "Python", "Java"].length

a) 0
b) 3
c) 4
d) nil

Answer:

b) 3

Explanation:

This code returns the number of elements in the array, which is 3.

5. Which method in Ruby is used to convert a string to uppercase?

a) to_upper
b) uppercase
c) upcase
d) capitalize

Answer:

c) upcase

Explanation:

The upcase method is used in Ruby to convert all characters in a string to uppercase.

6. What is the output of "Ruby" * 3?

a) RubyRubyRuby
b) Ruby3
c) RubyRuby
d) Error

Answer:

a) RubyRubyRuby

Explanation:

In Ruby, the * operator when used with a string and a number repeats the string that number of times.

7. Which operator in Ruby is used for equality comparison?

a) =
b) ==
c) ===
d) equal?

Answer:

b) ==

Explanation:

The == operator in Ruby is used to check if two values are equal in content.

8. Which of the following is true about Ruby's nil?

a) It's equivalent to false.
b) It's an instance of NilClass.
c) It represents the absence of a value.
d) All of the above.

Answer:

d) All of the above.

Explanation:

nil in Ruby is an instance of the NilClass and represents the absence of a value. Moreover, in boolean contexts, only false and nil evaluate as false.

9. How do you comment out multiple lines in Ruby?

a) // … //
b) /* … */
c) # … #
d) You prefix each line with #.

Answer:

d) You prefix each line with #.

Explanation:

Ruby doesn’t have a multiple line comment feature. You have to prefix each line with the # symbol.

10. Which keyword is used to define a method in Ruby?

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

Answer:

d) def

Explanation:

In Ruby, the def keyword is used to define a method.

11. What will the following code return: 5.eql?(5.0)?

a) true
b) false
c) nil
d) Error

Answer:

b) false

Explanation:

The eql? method checks both the value and type. 5 is an integer and 5.0 is a float, so they are not equivalent.

12. Which of the following is a truthy value in Ruby?

a) false
b) nil
c) 0
d) ""

Answer:

c) 0

Explanation:

In Ruby, only false and nil are falsy. All other values, including 0 and "", are considered truthy.

13. How is a block defined in Ruby?

a) Using do..end
b) With {}
c) Both a and b
d) Using []

Answer:

c) Both a and b

Explanation:

Blocks in Ruby can be defined using both do..end and {} notations.

14. How do you catch exceptions in Ruby?

a) catch..throw
b) try..catch
c) rescue..begin
d) begin..rescue

Answer:

d) begin..rescue

Explanation:

In Ruby, the begin..rescue construct is used to handle exceptions.

15. Which operator is used for string concatenation?

a) +
b) &
c) ..
d) #

Answer:

a) +

Explanation:

The + operator is used for string concatenation in Ruby.

16. What is the super class of all classes in Ruby?

a) Root
b) Base
c) Object
d) Super

Answer:

c) Object

Explanation:

In Ruby, every class implicitly inherits from the Object class.

17. Which method is NOT defined in Ruby's Array class?

a) first
b) pick
c) last
d) length

Answer:

b) pick

Explanation:

There's no pick method for arrays in Ruby by default.

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

a) The current file
b) The current method
c) The current object
d) The superclass

Answer:

c) The current object

Explanation:

In Ruby, self refers to the object on which the current method is being executed.

19. Which of the following methods returns the reversed version of a string without modifying the original string?

a) upturn
b) reverse
c) backwards
d) invert

Answer:

b) reverse

Explanation:

The reverse method returns a new string that is a reversed version of the original.

20. How do you represent a range from 1 to 5 in Ruby?

a) 1..5
b) 1-5
c) 1:5
d) 1…5

Answer:

a) 1..5

Explanation:

In Ruby, 1..5 creates a range inclusive of both 1 and 5.

21. Which of the following loops isn't present in Ruby?

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

Answer:

c) do

Explanation:

Ruby doesn't have a standalone do loop. Instead, do is used to define blocks.

22. How do you define a global variable in Ruby?

a) Using the global keyword
b) Prefixing with $
c) Prefixing with @
d) Prefixing with @@

Answer:

b) Prefixing with $

Explanation:

Global variables in Ruby are prefixed with the $ sign.

23. What does the attr_accessor keyword do in Ruby?

a) Creates a private method
b) Provides read and write access for a variable
c) Executes a block
d) Provides read-only access for a variable

Answer:

b) Provides read and write access for a variable

Explanation:

attr_accessor creates both a getter and a setter method for a given variable.

24. What is the result of the expression nil || "hello"?

a) nil
b) true
c) hello
d) false

Answer:

c) hello

Explanation:

In Ruby, the || operator returns the first truthy value it encounters. Since nil is falsy, the string "hello" is returned.

25. Which of the following can be used to add an element to the end of an array?

a) push
b) <<
c) add
d) Both a and b

Answer:

d) Both a and b

Explanation:

In Ruby, both the push method and the << operator can be used to add an element to the end of an array.

26. Which of the following is NOT a valid way to define a symbol in Ruby?

a) :hello
b) hello:
c) :"hello world"
d) :'hello world'

Answer:

b) hello:

Explanation:

The correct ways to define a symbol in Ruby include :hello, :"hello world", and :'hello world'.

27. What is the main difference between the puts and print methods?

a) puts returns nil, while print returns the string it printed.
b) print adds a newline character at the end, while puts doesn't.
c) puts adds a newline character at the end, while print doesn't.
d) There is no difference.

Answer:

c) puts adds a newline character at the end, while print doesn't.

Explanation:

puts stands for "put string" and automatically appends a newline character, whereas print does not.

28. How is an instance variable defined in Ruby?

a) By prefixing with a @@
b) By prefixing with a @
c) By using the instance keyword
d) By prefixing with a #

Answer:

b) By prefixing with a @

Explanation:

Instance variables in Ruby are prefixed with the @ sign.

29. Which of the following methods returns the number of characters in a string?

a) size
b) count
c) length
d) Both a and c

Answer:

d) Both a and c

Explanation:

In Ruby, both the size and length methods return the number of characters in a string.

30. Which of the following is not a loop in Ruby?

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

Answer:

c) repeat

Explanation:

Ruby doesn't have a built-in repeat loop.

31. How do you create a new empty hash in Ruby?

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

Answer:

d) Both a and b

Explanation:

In Ruby, you can create a new empty hash either using the literal {} or using Hash.new.

32. What is the result of "hello".class in Ruby?

a) String
b) Object
c) Text
d) Char

Answer:

a) String

Explanation:

In Ruby, the .class method returns the class of an object. The class of the string "hello" is String.

33. How do you convert a string "123" into an integer in Ruby?

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

Answer:

d) Both a and c

Explanation:

In Ruby, you can convert a string to an integer using the to_i method of the string or using the Integer() method.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top