Bird
0
0

Why does compact! return nil sometimes instead of an array?

hard📝 Conceptual Q10 of 15
Ruby - Arrays

Why does compact! return nil sometimes instead of an array?

ABecause the array is empty
BBecause the method always returns nil
CBecause no <code>nil</code> values were found to remove
DBecause compact! is not a valid method
Step-by-Step Solution
Solution:
  1. Step 1: Understand compact! return behavior

    compact! returns nil if it did not remove any nil values from the array.
  2. Step 2: Reason why this happens

    If the array has no nil values, there is nothing to remove, so compact! returns nil to indicate no change.
  3. Final Answer:

    Because no nil values were found to remove -> Option C
  4. Quick Check:

    compact! returns nil if no nil removed = A [OK]
Quick Trick: compact! returns nil if no nil removed [OK]
Common Mistakes:
  • Thinking compact! always returns array
  • Confusing nil return with error
  • Assuming empty array causes nil return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes