Skip to content

Instantly share code, notes, and snippets.

@rozer007
Last active November 25, 2021 07:53
Show Gist options
  • Save rozer007/95d200bc9c726b3a4264f47bf4d64dc0 to your computer and use it in GitHub Desktop.
Save rozer007/95d200bc9c726b3a4264f47bf4d64dc0 to your computer and use it in GitHub Desktop.
Arrays in ruby
Q1) How can we find if a elements is present in an array or not ?
ans: array_name.include?(element)
Q2) How to add a element at the starting of the array?
ans: array_name.unshift(element)
Q3) Initialize the array with a particular elements while creating an array?
ans: array_name=Array.new(size,initial value)
Q4) How to find the permutation of an array ?
ans: array_name.permutation("the size of permutation")
Q1) What is the correct way to create array of size 10?
a) array=Array.new()=10
b) array=Array.new(10)
c) array=Array.new()=[10]
d) None of these
ans: b) array=Array.new(10)
Q2) What will be the output of this statement :
array1 = [[1,2,3,5],[0,0,1,0]]
array2 = [[1,2,3,5],[0,1,0,0]]
print array1-array2
a) [[0, 0, 1, 0]]
b) [[1, 2, 3, 5], [0, 0, 1, 0]]
c) nil
d) can't determined
ans: a)[[ 0, 0, 1, 0]]
Q3) What is the output of this statement :
n = [ 80, 69, 80 ]
puts n.pack("ccc")
a) EPE
b) epe
c) PEP
d) pep
ans: c) PEP
Q4) What will this statements give as output :
array = [12,13,14,15,16]
print array[6]
a) Syntax error
b) Array index out of bound
c) nil
d) [nil]
ans: c)nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment