Ruby Questions and Answers Part-20

1. 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

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

2. 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

Answer: c
Explanation: Number of elements in both the arrays are not same hence they are unequal
Output:
Not equal

3. 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

Answer: b
Explanation: Number of elements in both the arrays are same hence they are unequal
Output:
Equal

4. 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

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]]

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]]
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

Answer: c
Explanation: We get an empty array by subtracting two arrays of same elements
Output:
[]

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) [].
d) Error

Answer: d
Explanation: We can’t directly multiply elements of array, it will show an error
Output:
can't convert Array into Integer

7. 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

Answer: a
Explanation: Anding two arrays will give the common elements in both the arrays.
Output:
[[1, 2, 3], [0, 0, 0]]

8. 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

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]]

9. 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

Answer: b
Explanation: The negation of the given array is not possible hence the result is false.
Output:
False

10. 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

Answer: a
Explanation: ‘+’ will append both the arrays.
Output:
[["a", "b"], ["e", "a"]]