Bird
0
0

Given arr = [3, 4], which code snippet results in [1, 2, 3, 4, 5]?

hard📝 Application Q8 of 15
Ruby - Arrays
Given arr = [3, 4], which code snippet results in [1, 2, 3, 4, 5]?
Aarr.pop(1, 2).unshift(5)
Barr.push(1, 2).unshift(5)
Carr.shift(1, 2).push(5)
Darr.unshift(1, 2).push(5)
Step-by-Step Solution
Solution:
  1. Step 1: Add 1 and 2 at start with unshift

    arr.unshift(1, 2) adds 1 and 2 at front, array becomes [1, 2, 3, 4].
  2. Step 2: Add 5 at end with push

    arr.push(5) adds 5 at end, array becomes [1, 2, 3, 4, 5].
  3. Final Answer:

    arr.unshift(1, 2).push(5) -> Option D
  4. Quick Check:

    unshift adds front, push adds end [OK]
Quick Trick: Chain unshift and push to add at front and end [OK]
Common Mistakes:
MISTAKES
  • Using pop or shift incorrectly
  • Wrong order of operations
  • Misunderstanding method chaining

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes