Ruby array MCQ
11. 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.
12. 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.
13. 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.
14. 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
15. 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
16. 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.
17. 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.
18. 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.
19. 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
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