Array Methods Flashcards

1
Q

What will the following array method return?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

famous_cats.include?(“Maru”)

A

True

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

How would you create the following array without hardcoding?

[1, 2, 3, 4, 5]

A

(1..5).to_a

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

How would I check to see if the array included “lil bub”?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

A

famous_cats.include?(“lil bub”)

Should return true

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

How would I check to see the number of elements in an array? (There are two ways)

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

A

famous_cats.size
OR
famous_cats.length

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

How would you create a new array called my_array using the class constructor?

A

my_array = Array.new

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

How would you grab the index of “Maru”?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

A

famous_cats.index(“Maru”)

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

How would you insert the item “nala cat” at the beginning of the array?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

A

famous_cats.unshift(“nala cat”)

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

How would you create a new array called my_array using the literal constructor?

A

my_array = [ ]

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

What will the following array method return?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

famous_cats.length

A

3

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

How would you retrieve the first item of an array? (There are two ways)

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

A

famous_cats.first
OR
famous_cats[0]

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

What will the following array method return?

(1..10).to_a

A

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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

How would you make the array [“a”, “b”, “c”] looking like the following?

“a.b.c”

A

[“a”, “b”, “c”].join(“.”)

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

What will the following array method return?

[“a”, “b”, “c”].join

A

“abc”

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

How would you retrieve the second item in the array famous_cats?

famous_cats = [“Cheshire Cat”, “Puss in Boots”, “Garfield”]

A

famous_cats[1]

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

What will the following array method return?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

famous_cats.last

A

“Maru”

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

What will the following array method return?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

famous_cats.index(“grumpy cat”)

A

1

17
Q

What will the following array method return?

famous_wizards = [“Dumbledore”, “Gandalf”, “Merlin”]

famous_wizards.reverse

A

[“Merlin”, “Gandalf”, “Dumbledore”]

18
Q

What will the following array method return?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]
famous_cats.include?(“Garfield”)

A

False

19
Q

How would you insert the item “nala cat” at the end of the array? (There are two ways)

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

A

famous_cats &laquo_space;“nala cat”
OR
famous_cats.push(“nala cat”)

20
Q

How would you sort the following array?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

A

famous_cats.sort

21
Q

How would you remove the last item of the array?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

A

famous_cats.pop

22
Q

What will famous_cats look like after the code finishes running?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

famous_cats.pop

A

[“lil’ bub”, “grumpy cat”]

23
Q

What will famous_cats look like after the code finishes running?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

famous_cats &laquo_space;“nala cat”

A

[“lil’ bub”, “grumpy cat”, “Maru”, “nala cat”]

24
Q

What will the following array method return?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

famous_cats.size

A

3

25
Q

What will the following array method return?

[“a”, “b”, “c”].join(“ “)

A

“a b c”

26
Q

How would you retrieve the last item of an array?

famous_cats= [“lil’ bub”, “grumpy cat”, “Maru”]

A

famous_cats.last

27
Q

What will famous_cats look like after the code finishes running?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

famous_cats.shift

A

[“grumpy cat”, “Maru”]

28
Q

How would you reverse the following array?

famous_wizards = [“Dumbledore”, “Gandalf”, “Merlin”]

A

famous_wizards.reverse

29
Q

What will the following array method return?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

famous_cats.sort

A

[“grumpy cat”, “lil’ bub”, “Maru”]

30
Q

What will the following array method return?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

famous_cats.first

A

“lil bub”

31
Q

What will the following array method return?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

famous_cats.index(“Garfield”)

A

nil

32
Q

What will famous_cats look like after the code finishes running?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

famous_cats.push(“nala cat”)

A

[“lil’ bub”, “grumpy cat”, “Maru”, “nala cat”]

33
Q

What will famous_cats look like after the code finishes running?

famous_cats = [“lil’ bub”, “grumpy cat”, “Maru”]

famous_cats.unshift(“nala cat”)

A

[“nala cat”, “lil’ bub”, “grumpy cat”, “Maru”]