Bird
0
0

Identify the error in this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Arrays
Identify the error in this Ruby code snippet:
arr = [5, 2, 9]
arr.sort!
arr.reverse
puts arr.inspect
AMissing parentheses in method calls cause errors.
BThe <code>sort!</code> method is invalid syntax.
CThe array <code>arr</code> cannot be reversed after sorting.
DUsing <code>reverse</code> does not change <code>arr</code> in place.
Step-by-Step Solution
Solution:
  1. Step 1: Understand sort! effect

    sort! sorts the array arr in place, so arr becomes [2, 5, 9].
  2. Step 2: Check reverse method usage

    reverse returns a new reversed array but does not modify arr itself.
  3. Final Answer:

    Using reverse does not change arr in place. -> Option D
  4. Quick Check:

    reverse without ! returns new array [OK]
Quick Trick: Use reverse! to modify array in place [OK]
Common Mistakes:
  • Assuming reverse changes original array
  • Thinking sort! is invalid
  • Believing method calls need parentheses always

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes