Recall & Review
beginner Click to reveal answer
What does the
push method do in a Ruby array?The
push method adds one or more elements to the end of an array.beginner Click to reveal answer
How does the
pop method modify an array?The
pop method removes the last element from an array and returns it.beginner Click to reveal answer
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.beginner Click to reveal answer
What does the
unshift method do in a Ruby array?The
unshift method adds one or more elements to the beginning of an array.beginner Click to reveal answer
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.Which method removes the last element from a Ruby array?
✗ Incorrect
The
pop method removes and returns the last element of the array.What does
arr.push(4) do if arr = [1, 2, 3]?✗ Incorrect
push adds the element to the end of the array.Which method removes the first element from a Ruby array?
✗ Incorrect
shift removes and returns the first element of the array.What will
arr.unshift(0) do to arr = [1, 2, 3]?✗ Incorrect
unshift adds elements to the start of the array.If you want to add an element to the end of an array, which method should you use?
✗ Incorrect
Use
push to add elements to the end of an array.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.
