Bird
0
0

Identify the problem in this Ruby code:

medium📝 Debug Q7 of 15
Ruby - Arrays
Identify the problem in this Ruby code:
arr = [1, 2, 3]
arr.shift(2)
p arr
Ashift(2) removes first two elements correctly
Bpop should be used to remove multiple elements
Cshift does not accept arguments
Dshift(2) returns an error
Step-by-Step Solution
Solution:
  1. Step 1: Understand shift with argument

    In Ruby, shift(n) removes the first n elements and returns them.
  2. Step 2: Check code behavior

    arr.shift(2) removes first two elements, so array becomes [3].
  3. Final Answer:

    shift(2) removes first two elements correctly -> Option A
  4. Quick Check:

    shift(n) removes n elements from front [OK]
Quick Trick: shift(n) removes n elements from start [OK]
Common Mistakes:
  • Thinking shift can't take arguments
  • Using pop to remove from front
  • Expecting error from shift(2)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes