0
0
Rubyprogramming~5 mins

Array modification (push, pop, shift, unshift) in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the push method do in a Ruby array?
The push method adds one or more elements to the end of an array.
Click to reveal answer
beginner
How does the pop method modify an array?
The pop method removes the last element from an array and returns it.
Click to reveal answer
beginner
What is the difference between shift and pop in Ruby arrays?
shift removes the first element of the array, while pop removes the last element.
Click to reveal answer
beginner
What does the unshift method do in a Ruby array?
The unshift method adds one or more elements to the beginning of an array.
Click to reveal answer
beginner
If you have arr = [1, 2, 3] and you run arr.unshift(0), what will arr be?
The array will be [0, 1, 2, 3] because unshift adds the element 0 to the start.
Click to reveal answer
Which method removes the last element from a Ruby array?
Apop
Bshift
Cpush
Dunshift
What does arr.push(4) do if arr = [1, 2, 3]?
AAdds 4 to the beginning of the array
BRemoves the first element
CAdds 4 to the end of the array
DRemoves the last element
Which method removes the first element from a Ruby array?
Ashift
Bunshift
Cpush
Dpop
What will arr.unshift(0) do to arr = [1, 2, 3]?
ARemove the first element
BRemove the last element
CAdd 0 to the end
DAdd 0 to the beginning
If you want to add an element to the end of an array, which method should you use?
Apop
Bpush
Cshift
Dunshift
Explain how the methods push, pop, shift, and unshift modify a Ruby array.
Think about adding or removing elements from the start or end of the array.
You got /4 concepts.
    Describe a real-life situation where you might use shift and pop on an array.
    Imagine a line of people or a stack of objects.
    You got /2 concepts.