Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Arrays
What will be the output of this Ruby code?
arr = [1, 2, 3]
arr.pop
arr.unshift(0)
p arr
A[1, 2, 3, 0]
B[0, 1, 2, 3]
C[0, 1, 2]
D[1, 2]
Step-by-Step Solution
Solution:
  1. Step 1: Remove last element with pop

    arr.pop removes 3, so array becomes [1, 2].
  2. Step 2: Add 0 at the start with unshift

    arr.unshift(0) adds 0 at front, so array becomes [0, 1, 2].
  3. Final Answer:

    [0, 1, 2] -> Option C
  4. Quick Check:

    pop removes last, unshift adds first [OK]
Quick Trick: pop removes last, unshift adds first [OK]
Common Mistakes:
  • Confusing pop with shift
  • Adding element at wrong end
  • Misreading array after operations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes