Array Quiz
cowboy_bebop_characters = ["Spike Spiegel", "Jet Black", "Faye Valentine", "Ed", "Ein"]
Given the above array, how might one access "Ein"
? You may select more than one option.
cowboy_bebop_characters[4]
cowboy_bebop_characters[-1]
cowboy_bebop_characters[cowboy_bebop_characters.length-1]
cowboy_bebop_characters.last
cowboy_bebop_characters[4..-1][0]
"Ein"
(though some are more elegant than others). The
first option accesses "Ein"
by its index, the second accesses it as the last
element in the array, the third is equivalent to the first by supplying an
expression that evaluates to 4
, the fourth uses the last
method to return
the last element in the array, and the fifth accesses the first element of the
subarray ["Ein"]
. cowboy_bebop_characters = ["Spike Spiegel", "Jet Black", "Faye Valentine", "Ed", "Ein"]
Given the above array, how might one return ["Spike Spiegel", "Jet Black"]
? You may select more than one option.
cowboy_bebop_characters[0..1]
cowboy_bebop_characters[0...2]
cowboy_bebop_characters[-5..-4]
cowboy_bebop_characters[-5...-3]
[cowboy_bebop_characters.first, cowboy_bebop_characters[1]]
["Spike Spiegel", "Jet Black"]
(though some are more
elegant than others). The first and second options return a subarray of the
first two elements in the array (0..1 == 0...2
). The third and fourth options
return a subarray of the fifth- and fourth-to-last elements int he array
(-5..-4 == -5...-3
). The fifth option declares an array of the first and
second elements in the array. cowboy_bebop_characters = ["Spike Spiegel", "Jet Black", "Faye Valentine", "Ed", "Ein"]
popped = cowboy_bebop_characters.pop
cowboy_bebop_characters.unshift(popped)
What is the value of cowboy_bebop_characters
at the end of the above snippet?
["Spike Spiegel", "Jet Black", "Faye Valentine", "Ed", "Ein"]
["Spike Spiegel", "Jet Black", "Faye Valentine", "Ed"]
["Jet Black", "Faye Valentine", "Ed", "Ein", "Spike Spiegel"]
["Ein", "Spike Spiegel", "Jet Black", "Faye Valentine", "Ed"]
"Ein"
) and unshifts it
to the front of the array, making the array ["Ein", "Spike Spiegel", "Jet
Black", "Faye Valentine", "Ed"]
. cowboy_bebop_characters = ["Spike Spiegel", "Jet Black", "Faye Valentine", "Ed", "Ein"]
shifted = cowboy_bebop_characters.shift
cowboy_bebop_characters.push(shifted)
What is the value of cowboy_bebop_characters
at the end of the above snippet?
["Spike Spiegel", "Jet Black", "Faye Valentine", "Ed", "Ein"]
["Spike Spiegel", "Jet Black", "Faye Valentine", "Ed"]
["Jet Black", "Faye Valentine", "Ed", "Ein", "Spike Spiegel"]
["Ein", "Spike Spiegel", "Jet Black", "Faye Valentine", "Ed"]
"Spike Spiegel"
) and
pushes it to the back of the array, making the array ["Jet Black", "Faye
Valentine", "Ed", "Ein", "Spike Spiegel"]
. animated_characters = ["Spike Spiegel", "Jet Black"]
animated_characters << "Ein"
r_and_m = ["Rick", "Morty", "Summer"]
animated_characters = animated_characters + r_and_m
What is the value of animated_characters
at the end of the above snippet?
["Spike Spiegel", "Jet Black", "Ein", "Rick", "Morty", "Summer"]
["Spike Spiegel", "Jet Black", "Ein", ["Rick", "Morty", "Summer"]]
["Ein", "Spike Spiegel", "Jet Black", "Rick", "Morty", "Summer"]
["Spike Spiegel", "Jet Black", "Ein"]
animated_characters
becomes ["Spike Spiegel", "Jet Black",
"Ein"]
after "Ein" is shoveled into the array. Then animated_characters
is
concatenated with r_and_m
. Because animated_characters
is reassigned when
it's concatenated, the variable acquires the new value of ["Spike
Spiegel", "Jet Black", "Ein", "Rick", "Morty", "Summer"]
. animated_characters = ["Spike Spiegel", "Jet Black"]
animated_characters << "Ein"
r_and_m = ["Rick", "Morty", "Summer"]
animated_characters + r_and_m
What is the value of animated_characters
at the end of the above snippet?
["Spike Spiegel", "Jet Black", "Ein", "Rick", "Morty", "Summer"]
["Spike Spiegel", "Jet Black", "Ein", ["Rick", "Morty", "Summer"]]
["Ein", "Spike Spiegel", "Jet Black", "Rick", "Morty", "Summer"]
["Spike Spiegel", "Jet Black", "Ein"]
animated_characters
and r_and_m
are concatenated without reassignment. animated_characters
therefore retains the value ["Spike Spiegel", "Jet Black", "Ein"]
. animated_characters = ["Spike Spiegel", "Jet Black"]
animated_characters << "Ein"
r_and_m = ["Rick", "Morty", "Summer"]
animated_characters << r_and_m
What is the value of animated_characters
at the end of the above snippet?
["Spike Spiegel", "Jet Black", "Ein", "Rick", "Morty", "Summer"]
["Spike Spiegel", "Jet Black", "Ein", ["Rick", "Morty", "Summer"]]
["Ein", "Spike Spiegel", "Jet Black", "Rick", "Morty", "Summer"]
["Spike Spiegel", "Jet Black", "Ein"]
r_and_m
is shoveled
into animated_characters
rather than concatenated. r_and_m
is simply added
to the back of the array like any other element, creating a nested array:
["Spike Spiegel", "Jet Black", "Ein", ["Rick", "Morty", "Summer"]]
.["dolla", "dolla", "bills", "y'all"].join('$')
What does the above code snippet return?
["y'all", "dolla", "dolla", "bills"]
["dolla", "bills", "y'all"]
"dolla$dolla$bills$y'all"
"dolladollabillsy'all"
join
by definition combines every element of the array into a string, here
with the separator '$'
.["dolla", "dolla", "bills", "y'all"].join
What does the above code snippet return?
["y'all", "dolla", "dolla", "bills"]
["dolla", "bills", "y'all"]
"dolla$dolla$bills$y'all"
"dolladollabillsy'all"
join
is invoked with no separator argument; therefore, the elements of the
array are joined without spaces.["dolla", "dolla", "bills", "y'all"].sort.reverse
What does the above code snippet return?
["y'all", "dolla", "dolla", "bills"]
["dolla", "bills", "y'all"]
"dolla$dolla$bills$y'all"
"dolladollabillsy'all"
sort
method is first invoked on the array, returning ["bills", "dolla",
"dollar", "y'all"]
. Then the reverse
method is invoked on the return value of
sort
, returning ["y'all", "dolla", "dolla", "bills"]
.[1, 2, 3].include?(2)
What does the above code snippet return?
true
false
2
3
include?
by definition returns a boolean value indicating whether the argument
is included in the array or string (it is included).