Array Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How do you add chain push in an array?

A

a &laquo_space;1 &laquo_space;2 &laquo_space;3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does “cycle” method will do in an array?

A

a.cycle {|x| puts x} # print, a, b, c, a, b, c,.. forever.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which method is used to do binary search in an array?

A

bsearch

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the use of “minmax” method in an array?

A

Return both minimum and maximum values from an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

a = [[1, [2, 3]]].How will you extract the inner array value 3?

A

a.dig(0, 1, 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you remove the nested arrays?

A

a.flatten

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If I want to print the possible combinations(probability) repeatation of the array, which method I can use?

A

a.permutation.to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Differentiate these two methods

a) reverse b)reverse!

A

First one is notmal method

Second one is Bang method. It will overwrite the original value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain take_while method?

A

a = [1, 2, 3, 4, 5, 0]

a.take_while {|i| i < 3} #=> [1, 2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which method will

merges elements of self with corresponding elements from each argument.(index wise)

A

a = [ 4, 5, 6 ]
b = [ 7, 8, 9 ]
[1, 2, 3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How will you search through number of arrays for a specific item?

A

assoc method
s1 = [ “colors”, “red”, “blue”, “green” ]
s2 = [ “letters”, “a”, “b”, “c” ]
s3 = “foo”
a = [ s1, s2, s3 ]
a.assoc(“letters”) #=> [ “letters”, “a”, “b”, “c” ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain about initialize_copy method

A

Replaces the contents of self with the contents of other array, truncating or expanding if necessary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In how many ways items can be removed from array in Ruby?

A

Ruby array elements can be removed in different ways.

pop
shift
delete
uniq

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In how many ways items can be added in an array in Ruby?

A

Ruby array elements can be added in different ways.

push or «
unshift
insert

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are Ruby arrays and how they can be created?

A

A Ruby array is created in many ways.

Using literal constructor [] 
a = []
Using new class method
a = Array.new
How well did you know this?
1
Not at all
2
3
4
5
Perfectly