Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Arrays
What will be the output of the following Ruby code?
arr = [1, 2, 3]
arr.push(4)
arr.shift
p arr
A[1, 2, 3]
B[1, 2, 3, 4]
C[4, 1, 2, 3]
D[2, 3, 4]
Step-by-Step Solution
Solution:
  1. Step 1: Execute push(4) on array

    Adding 4 to the end changes array from [1, 2, 3] to [1, 2, 3, 4].
  2. Step 2: Execute shift to remove first element

    Removing first element (1) results in [2, 3, 4].
  3. Final Answer:

    [2, 3, 4] -> Option D
  4. Quick Check:

    push adds end, shift removes front = [2, 3, 4] [OK]
Quick Trick: push adds end, shift removes front, watch order [OK]
Common Mistakes:
  • Ignoring shift effect
  • Thinking push adds front
  • Confusing pop with shift

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes