Q. In Ruby, Array index -1 represent ______.

  • (A) First Element
  • (B) Last element
  • (C) Middle element
  • (D) Reverse the array
πŸ’¬ Discuss
βœ… Correct Answer: (B) Last element
Explanation: A negative index is assumed relative to the end of the array --- that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.

Q. Which of the following is correct syntax to create an array in ruby?

  • (A) names = Array.new
  • (B) names = Array.new(20)
  • (C) Both A and B
  • (D) None of the above
πŸ’¬ Discuss
βœ… Correct Answer: (C) Both A and B
Explanation: We can create an array using both option A and option B.

Q. What will be the output of the given ruby code?

Code:
digits = Array(0...9)
puts #{digits}
  • (A) [0, 1, 2, 3, 4, 5, 6, 7, 8]
  • (B) [0, 1, 2, 3, 4, 5, 6, 7, 8]
  • (C) [0, 1, 2, 3, 4, 5, 6, 7, 8,9]
  • (D) [1, 2, 3, 4, 5, 6, 7, 8]
πŸ’¬ Discuss
βœ… Correct Answer: (B) [0, 1, 2, 3, 4, 5, 6, 7, 8]
Explanation: This will produce the following result : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Q. What will be the output of the given ruby code?

Code:
digits = Array(0..9)
num = digits.at(6)
puts #{num}
  • (A) 5
  • (B) 6
  • (C) 7
  • (D) 8
πŸ’¬ Discuss
βœ… Correct Answer: (B) 6
Explanation: This will produce the following result : 6

Q. What will be the output of the given ruby code?

  • (A) ABC
  • (B) abc
  • (C) BCD
  • (D) bcd
πŸ’¬ Discuss
βœ… Correct Answer: (A) ABC
Explanation: This will produce the following result : ABC

Q. What will be the output of the given ruby code?

Code:
a = [ a, b, c ] puts a.pack(a3a3a3)
  • (A) ABC
  • (B) abc
  • (C) BCD
  • (D) bcd
πŸ’¬ Discuss
βœ… Correct Answer: (B) abc
Explanation: This will produce the following result : abc

Q. What will be the output of the given ruby code?

Code:
arr = [1, 2, 3, 4]
print arr
  • (A) [1, 2, 3, 4]
  • (B) 1234
  • (C) error
  • (D) Infinite Loop
πŸ’¬ Discuss
βœ… Correct Answer: (A) [1, 2, 3, 4]
Explanation: A variable arr is declared and [1, 2, 3, 4] is stored in that variable.

Q. What will be the output of the given ruby code?

Code:
array = [100, 200, 300, 400, 500]
print array[4]
  • (A) [100, 200, 300, 400, 500]
  • (B) 300
  • (C) 400
  • (D) 500
πŸ’¬ Discuss
βœ… Correct Answer: (D) 500
Explanation: Array's index start from 0 so array[4] will give 500.

Q. What will be the output of the given ruby code?

Code:
string_array = [a,e,i,o,u]
print string_array
  • (A) Error
  • (B) ["a","e","i","o","u"]
  • (C) aeiou
  • (D) Infinite Loop
πŸ’¬ Discuss
βœ… Correct Answer: (B) ["a","e","i","o","u"]
Explanation: The array is a string array.

Q. What will be the output of the given ruby code?

Code:
string_array = [a,e,i,o,u]
print string_array[3]
  • (A) ["a","e","i","o","u"]
  • (B) e
  • (C) i
  • (D) o
πŸ’¬ Discuss
βœ… Correct Answer: (D) o
Explanation: The array is a string array and the index is 3 so 'o' will be the output.