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?
Answer:
Explanation:
Yukihiro Matsumoto, also known as "Matz", is the creator of Ruby.
2. Which method checks if an array is empty?
Answer:
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?
Answer:
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
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
In Ruby, the def keyword is used to define a method.
11. What will the following code return: 5.eql?(5.0)?
Answer:
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?
Answer:
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?
Answer:
Explanation:
Blocks in Ruby can be defined using both do..end and {} notations.
14. How do you catch exceptions in Ruby?
Answer:
Explanation:
In Ruby, the begin..rescue construct is used to handle exceptions.
15. Which operator is used for string concatenation?
Answer:
Explanation:
The + operator is used for string concatenation in Ruby.
16. What is the super class of all classes in Ruby?
Answer:
Explanation:
In Ruby, every class implicitly inherits from the Object class.
17. Which method is NOT defined in Ruby's Array class?
Answer:
Explanation:
There's no pick method for arrays in Ruby by default.
18. What does the self keyword refer to in Ruby?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
Global variables in Ruby are prefixed with the $ sign.
23. What does the attr_accessor keyword do in Ruby?
Answer:
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"?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
Instance variables in Ruby are prefixed with the @ sign.
29. Which of the following methods returns the number of characters in a string?
Answer:
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?
Answer:
Explanation:
Ruby doesn't have a built-in repeat loop.
31. How do you create a new empty hash in Ruby?
Answer:
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?
Answer:
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?
Answer:
Explanation:
In Ruby, you can convert a string to an integer using the to_i method of the string or using the Integer() method.