0
0
Rubyprogramming~20 mins

Array modification (push, pop, shift, unshift) in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code?
Consider the following Ruby code snippet. What will be printed after all operations?
Ruby
arr = [1, 2, 3]
arr.push(4)
arr.shift
puts arr.inspect
A[4, 1, 2, 3]
B[1, 2, 3, 4]
C[2, 3, 4]
D[1, 2, 3]
Attempts:
2 left
💡 Hint
Remember that push adds to the end and shift removes from the start.
Predict Output
intermediate
2:00remaining
What does this Ruby code print?
Look at this code. What will be the output?
Ruby
arr = ['a', 'b', 'c']
arr.unshift('z')
arr.pop
puts arr.inspect
A["z", "a", "b"]
B["a", "b", "c", "z"]
C["z", "a", "b", "c"]
D["a", "b"]
Attempts:
2 left
💡 Hint
unshift adds at the start, pop removes from the end.
Predict Output
advanced
2:00remaining
What is the final array after these operations?
Analyze the code and determine the final array content.
Ruby
arr = [10, 20, 30, 40]
arr.pop
arr.unshift(5)
arr.push(50)
arr.shift
puts arr.inspect
A[10, 20, 30, 50]
B[20, 30, 50]
C[5, 10, 20, 30]
D[5, 10, 20, 30, 50]
Attempts:
2 left
💡 Hint
Track each operation step-by-step.
Predict Output
advanced
2:00remaining
What error does this Ruby code raise?
Examine the code and identify the error it produces when run.
Ruby
arr = [1, 2, 3]
arr.pop(-5)
puts arr.inspect
AIndexError
BArgumentError
CNo error, outputs [1]
DTypeError
Attempts:
2 left
💡 Hint
Check the Ruby documentation for pop method arguments.
🧠 Conceptual
expert
2:00remaining
How many elements are in the array after these operations?
Given the code below, how many elements does the array contain at the end?
Ruby
arr = []
arr.unshift(1)
arr.push(2)
arr.unshift(3)
arr.pop
arr.shift
arr.push(4)
arr.shift
A2
B0
C3
D1
Attempts:
2 left
💡 Hint
Count carefully after each operation.