Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Arrays
What is the output of this Ruby code?
arr = [5, 6, 7]
arr.shift
arr.push(8)
p arr
A[6, 7, 8]
B[5, 6, 7, 8]
C[8, 5, 6, 7]
D[7, 8]
Step-by-Step Solution
Solution:
  1. Step 1: Remove first element with shift

    arr.shift removes 5, so array becomes [6, 7].
  2. Step 2: Add 8 at the end with push

    arr.push(8) adds 8, so array becomes [6, 7, 8].
  3. Final Answer:

    [6, 7, 8] -> Option A
  4. Quick Check:

    shift removes first, push adds last [OK]
Quick Trick: shift removes first, push adds last [OK]
Common Mistakes:
MISTAKES
  • Forgetting shift removes first element
  • Assuming push adds at start
  • Mixing order of operations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes