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. Each element in an array is associated with _______.
A. Numbers
B. Index
C. Integer
D. None of the above
View Answer
Ans : B
Explanation: Each element in an array is associated with and referred to by an index.
3. 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.
4. Array index -1 represent ______.
A. First Element
B. Middle element
C. Last element
D. Reverse the array
View Answer
Ans : C
Explanation: A negative index is assumed relative to the end of the array --- that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.
5. 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.
6. Which of the following command is used to tell the size of array?
A. arraynames.size
B. arraynames.length
C. arraynames.len
D. Both A and B
View Answer
Ans : D
Explanation: Both Option A and Option B are correct.
7. 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]
8. What will be the output of the given code?
digits = Array(0..9)
num = digits.at(6)
puts #{num}
A. 5
B. 6
C. 7
D. 8
View Answer
Ans : B
Explanation: This will produce the following result : 6
9. 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
10. What will be the output of the given code?
a = [ a, b, c ] puts a.pack(a3a3a3)
A. ABC
B. abc
C. BCD
D. bcd
View Answer
Ans : B
Explanation: This will produce the following result : abc
11. 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.
12. What will be the output of the given code?
array = [100, 200, 300, 400, 500]
print array[4]
A. [100, 200, 300, 400, 500]
B. 300
C. 400
D. 500
View Answer
Ans : D
Explanation: Array's index start from 0 so array[4] will give 500.
13. 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.
14. What will be the output of the given code?
string_array = [a,e,i,o,u]
print string_array[3]
A. ["a","e","i","o","u"]
B. e
C. i
D. o
View Answer
Ans : D
Explanation: The array is a string array and the index is 3 so 'o' will be the output.
15. 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.
16. What is the output of the given code?
a=[[a,b]]
b=[[e,a]]
print a + b
A. [["a", "b"], ["e", "a"]].
B. [["2a", "b"], ["e"]].
C. TRUE
D. FALSE
View Answer
Ans : A
Explanation: '+' will append both the arrays.
17. 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.
18. What will be the output of the given code?
a=[1,2,3,4,5]
b=[1,2,4,6,8]
if a[3]==b[2]
print Equal
end
A. TRUE
B. FALSE
C. Equal
D. error
View Answer
Ans : C
Explanation: a[3]=4 and b[2]=4 hence it will print equal according to the given if condition
19. 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
20. What is the output of the given code?
array1 = [[1,2,3,4,5],[0,0,0,0]]
print !array1
A. Error
B. [[1, 2, 3, 4, 5], [0, 0, 0, 0]].
C. TRUE
D. FALSE
View Answer
Ans : D
Explanation: The negation of the given array is not possible hence the result is false.
Discussion