Ruby Programming Questions and Answers – Arrays of Arrays

«
»

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

Answer: a
Explanation: We can put anything in an array and make it array of arrays.
advertisement

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

Answer: a
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

Answer: a
Explanation: Array inside array is declared and then printed and .each is the iterator used.

advertisement
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

Answer: c
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?

advertisement
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

Answer: b
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

Answer: b
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?

advertisement
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

Answer: c
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

Answer: d
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

Answer: a
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

Answer: b
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

Answer: b
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

Answer: a
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.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn