Ruby Programming Questions and Answers – Arrays

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

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

Answer: a
Explanation: A variable my_array is declared and [1, 2, 3, 4] is stored in that variable.

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

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

4. What will be the output of the following?

advertisement
array = [100, 200, 300, 400, 500]
print array[4]

a) 400
b) 500
c) Nil
d) None of the mentioned
View Answer

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

advertisement
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

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

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

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

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

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

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

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

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.