Bird
0
0

Find the error in this Ruby code snippet:

medium📝 Debug Q6 of 15
Ruby - Methods
Find the error in this Ruby code snippet:
arr = [1, 2, 3]
arr.sort!
puts arr
AMethod <code>sort!</code> does not exist for arrays.
BNo error; code works correctly.
CMissing parentheses in <code>sort!</code> call.
DCannot call bang methods on arrays.
Step-by-Step Solution
Solution:
  1. Step 1: Check if sort! exists for arrays

    Ruby arrays have a sort! method that sorts the array in place.
  2. Step 2: Verify syntax correctness

    Calling arr.sort! without parentheses is valid syntax in Ruby.
  3. Final Answer:

    No error; code works correctly. -> Option B
  4. Quick Check:

    sort! exists and works on arrays [OK]
Quick Trick: Bang methods can be called without parentheses [OK]
Common Mistakes:
  • Assuming sort! does not exist
  • Thinking parentheses are mandatory
  • Believing bang methods can't be used on arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes