Ruby Questions and Answers Part-18

1. Each element in an array has an index and the starting index is index 1.
a) True
b) False

Answer: b
Explanation: Array’s starting index is index 0 not index 1.

2. What will be the output of the following?
array = [100, 200, 300, 400, 500]
print array[4]
a) 400
b) 500
c) Nil
d) None of the mentioned

Answer: b
Explanation: Array’s index start from 0 so array[4] will give 500.
Output:
500

3. What will be the output of the following?
array = [100, 200, 300, 400, 500]
print array[5]
a) 400
b) 500
c) Nil
d) None of the mentioned

Answer: c
Explanation: Array’s index start from 0 so array[5] will give nothing.
Output:
Nil

4. What will be the output of the following?
array = [100, 200, 300, 400, 500]
print "array[5]"
a) array[5].
b) 500
c) Nil
d) None of the mentioned

Answer: a
Explanation: Anything in double quotes is treated as string.
Output:
array[5]

5. What will be the output of the following?
array1 = [100, 200, 300, 400, 500]
array2 = [1,2,3,4,5]
if array1 == array2
print "They are equal"
else
print "Not equal"
end
a) They are equal
b) Not equal
c) Nil
d) None of the mentioned

Answer: b
Explanation: Two arrays are said to be equal if each and every element of both the arrays are equal.
Output:
Not equal

6. What will be the output of the following?
array1 = [0,0,0]
array2 = [0,0,0]
if array1 == array2
print "They are equal"
else
print "Not equal"
end
a) They are equal
b) Not equal
c) Nil
d) None of the mentioned

Answer: a
Explanation: Two arrays are said to be equal if each and every element of both the arrays are equal.
Output:
They are equal

7. What will be the output of the following?
array1 = [1,2,3]
array2 = [0,0,0]
if array1 >= array2
print "Greater or equal"
else
print "Not equal"
end
a) Greater or equal
b) Not equal
c) Error
d) None of the mentioned

Answer: c
Explanation: Elements of two arrays can’t be compared.
Output:
undefined method `>=' for [1, 2, 3]:Array

8. What will be the output of the following?
array1 = [1,2,3]
array2 = [0,0,0]
if array1 == array2
print "Equal"
else
print "Not equal"
end
a) Equal
b) Not equal
c) Error
d) None of the mentioned

Answer: a
Explanation: All the elements must be equal
Output:
Equal

9. It is possible to make array of booleans.
a) True
b) False

Answer: a
Explanation: Arrays of strings, numbers, booleans can be made.

10. What is the output of the given code?
string_array = ["a","e","i","o","u"]
print string_array
a) [“a”,”e”,”i”,”o”,”u”].
b) Error
c) Vowels
d) None of the mentioned

Answer: a
Explanation: The array is a string array.
Output:
["a","e","i","o","u"]