This set of Ruby Programming Multiple Choice Questions & Answers (MCQs) focuses on “Arrays of Arrays”.
1. Array of arrays are called multidimensional arrays.
a) True
b) False
View Answer
Explanation: We can put anything in an array and make it array of arrays.
2. What is the output of the given code?
multi_d_array = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] print multi_d_array
a) [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]].
b) [0, 0, 0, 0].
c) [0, 0, 0, 0][0, 0, 0, 0].
d) None of the mentioned
View Answer
Explanation: Array inside array is declared and then printed.
Output: [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
3. What is the output of the given code?
multi_d_array = [[1,2,3,4],[0,0,0,0]] multi_d_array.each { |x| puts "#{x}\n" }
a) [1, 2, 3, 4].
[0, 0, 0, 0].
[[1, 2, 3, 4], [0, 0, 0, 0]].
b) [0, 0, 0, 0].
c) [0, 0, 0, 0][0, 0, 0, 0].
d) None of the mentioned
View Answer
Explanation: Array inside array is declared and then printed and .each is the iterator used.
Output: [1, 2, 3, 4] [0, 0, 0, 0] [[1, 2, 3, 4], [0, 0, 0, 0]]
4. What is the output of the given code?
array1 = [[1,2,3,4],[0,0,0,0]] array2 = [[1,2,3],[0,0,0,0]] if array1==array2 print "Equal" else print "Not equal" end
a) [[1, 2, 3, 4], [0, 0, 0, 0]].
b) Equal
c) Not equal
d) None of the mentioned
View Answer
Explanation: Number of elements in both the arrays are not same hence they are unequal
Output: Not equal
5. 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]] if array1==array2 print "Equal" else print "Not equal" end
a) [[1, 2, 3, 4], [0, 0, 0, 0]].
b) Equal
c) Not equal
d) None of the mentioned
View Answer
Explanation: Number of elements in both the arrays are same hence they are unequal
Output: Equal
6. 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) [[2,4,6,8],[0,0,0,0]].
d) None of the mentioned
View Answer
Explanation: By adding two arrays we mean appending those two arrays.
Output: [[1, 2, 3, 4], [0, 0, 0, 0], [1, 2, 3, 4], [0, 0, 0, 0]]
7. 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) None of the mentioned
View Answer
Explanation: We get an empty array by subtracting two arrays of same elements
Output: []
8. 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) Error
View Answer
Explanation: We can’t directly multiply elements of array, it will show an error
Output: can't convert Array into Integer
9. What is the output of the given code?
array1 = [[1,2,3,4],[0,0,0,0]] array2 = [[1,2,3],[0,0,0]] print array1 && array2
a) [[1, 2, 3], [0, 0, 0]].
b) [[1, 2, 3, 4], [0, 0, 0, 0], [1, 2, 3, 4], [0, 0, 0, 0]].
c) [].
d) Error
View Answer
Explanation: Anding two arrays will give the common elements in both the arrays.
Output: [[1, 2, 3], [0, 0, 0]]
10. What is the output of the given code?
array1 = [[1,2,3,4,5],[0,0,0,0]] array2 = [[1,2,3],[0,0,0]] print array1 || array2
a) [[1, 2, 3], [0, 0, 0]].
b) [[1, 2, 3, 4, 5], [0, 0, 0, 0]].
c) [].
d) Error
View Answer
Explanation: Oring two arrays will give the maximum number common and uncommon elements in both the arrays.
Output: [[1, 2, 3, 4, 5], [0, 0, 0, 0]]
11. What is the output of the given code?
array1 = [[1,2,3,4,5],[0,0,0,0]] array2 = [[1,2,3],[0,0,0]] print !array1
a) [[1, 2, 3], [0, 0, 0]].
b) [[1, 2, 3, 4, 5], [0, 0, 0, 0]].
c) False
d) Error
View Answer
Explanation: The negation of the given array is not possible hence the result is false.
Output: False
12. 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) False
d) Error
View Answer
Explanation: ‘+’ will append both the arrays.
Output: [["a", "b"], ["e", "a"]]
Sanfoundry Global Education & Learning Series – Ruby Programming.
To practice all areas of Ruby Programming, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Programming MCQs
- Check Information Technology Books
- Apply for Programming Internship
- Check Ruby Programming Books