This set of Ruby Programming Multiple Choice Questions & Answers (MCQs) focuses on “Arrays”.
1. Arrays can be used to store multiple values in one single variable.
a) True
b) False
View Answer
Explanation: We can store multiple value in one single variable known as arrays.
2. What will be output of the given code?
my_array = [1, 2, 3, 4] print my_array
a) [1, 2, 3, 4].
b) 1234
c) Error
d) None of the mentioned
View Answer
Explanation: A variable my_array is declared and [1, 2, 3, 4] is stored in that variable.
Output: [1, 2, 3, 4]
3. Each element in an array has an index and the starting index is index 1.
a) True
b) False
View Answer
Explanation: Array’s starting index is index 0 not index 1.
4. 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
View Answer
Explanation: Array’s index start from 0 so array[4] will give 500.
Output: 500
5. 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
View Answer
Explanation: Array’s index start from 0 so array[5] will give nothing.
Output: Nil
6. 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
View Answer
Explanation: Anything in double quotes is treated as string.
Output: array[5]
7. 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
View Answer
Explanation: Two arrays are said to be equal if each and every element of both the arrays are equal.
Output: Not equal
8. 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
View Answer
Explanation: Two arrays are said to be equal if each and every element of both the arrays are equal.
Output: They are equal
9. 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
View Answer
Explanation: Elements of two arrays can’t be compared.
Output: undefined method `>=' for [1, 2, 3]:Array
10. 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
View Answer
Explanation: All the elements must be equal
Output: Equal
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.
- Check Information Technology Books
- Apply for Programming Internship
- Check Ruby Programming Books
- Practice Programming MCQs