Bird
0
0

Why does calling flatten on an array not change the original array in Ruby?

hard📝 Conceptual Q10 of 15
Ruby - Arrays
Why does calling flatten on an array not change the original array in Ruby?
ABecause flatten only works on strings, not arrays
BBecause flatten returns a new array and does not modify the original
CBecause flatten modifies the original array in place
DBecause flatten requires a block to modify the array
Step-by-Step Solution
Solution:
  1. Step 1: Understand flatten behavior

    The flatten method returns a new array with nested arrays merged but does not change the original array.
  2. Step 2: Differentiate from flatten!

    flatten! modifies the original array, but flatten returns a new one.
  3. Final Answer:

    Because flatten returns a new array and does not modify the original -> Option B
  4. Quick Check:

    flatten returns new array, original unchanged [OK]
Quick Trick: Use flatten! to modify original, flatten returns new array [OK]
Common Mistakes:
MISTAKES
  • Thinking flatten modifies original array
  • Confusing flatten with flatten!
  • Believing flatten works only on strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes