Ruby MCQ On Array
This section focuses on "Array" in Ruby. These Multiple Choice Questions (MCQ) should be practiced to improve the Ruby skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive examinations.
1. Ruby arrays are ____________.
A. unordered
B. ordered
C. Both A and B
D. None of the above
View Answer
Ans : B
Explanation: Ruby arrays are ordered, integer-indexed collections of any object.
2. Array indexing in ruby starts at_________.
A. -1
B. 1
C. 0
D. Random number
View Answer
Ans : C
Explanation: Array indexing starts at 0, as in C or Java.
3. Which of the following is correct syntax to create an array in ruby?
A. names = Array.new
B. names = Array.new(20)
C. Both A and B
D. None of the above
View Answer
Ans : C
Explanation: We can create an array using both option A and option B.
4. What will be the output of the given code?
digits = Array(0...9)
puts #{digits}
A. [0, 1, 2, 3, 4, 5, 6, 7, 8]
B. [0, 1, 2, 3, 4, 5, 6, 7, 8]
C. [0, 1, 2, 3, 4, 5, 6, 7, 8,9]
D. [1, 2, 3, 4, 5, 6, 7, 8]
View Answer
Ans : B
Explanation: This will produce the following result : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5. What will be the output of the given code?
n = [ 65, 66, 67 ] puts n.pack(ccc)
A. ABC
B. abc
C. BCD
D. bcd
View Answer
Ans : A
Explanation: This will produce the following result : ABC
6. What will be the output of the given code?
arr = [1, 2, 3, 4]
print arr
A. [1, 2, 3, 4]
B. 1234
C. error
D. Infinite Loop
View Answer
Ans : A
Explanation: A variable arr is declared and [1, 2, 3, 4] is stored in that variable.
7. What will be the output of the given code?
string_array = [a,e,i,o,u]
print string_array
A. Error
B. ["a","e","i","o","u"]
C. aeiou
D. Infinite Loop
View Answer
Ans : B
Explanation: The array is a string array.
8. What will be the output of the given code?
arr = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
print arr
A. [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]].
B. [0, 0, 0, 0][0, 0, 0, 0].
C. [0, 0, 0, 0].
D. error
View Answer
Ans : A
Explanation: Array inside array is declared and then printed.
9. What is the output of the given code?
array = [100, 200, 300, 400, 500]
print array[5]
A. 500
B. Syntax Error
C. Nil
D. Name Error
View Answer
Ans : C
Explanation: Array's index start from 0 so array[5] will give nothing.
10. What is the output of the given code?
array1 = [[1,2,3,4],[0,0,0,0]]
array2 = [[1,2,3,4],[0,0,0,0]]
print array1-array2
A. [[1, 2, 3, 4], [0, 0, 0, 0]]
B. [[1, 2, 3, 4], [0, 0, 0, 0], [1, 2, 3, 4], [0, 0, 0, 0]]
C. []
D. Nil
View Answer
Ans : C
Explanation: We get an empty array by subtracting two arrays of same elements
Discussion