Ruby - ArraysWhy does Array.new(3, []) cause unexpected behavior when modifying elements?ABecause the syntax is invalidBBecause Array.new cannot create arrays with default valuesCBecause all elements reference the same array objectDBecause the array size must be specified as a blockCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand default object behaviorArray.new(3, []) creates an array where all elements point to the same empty array object.Step 2: Effect on modificationModifying one element modifies all because they share the same object reference.Final Answer:Because all elements reference the same array object -> Option CQuick Check:Default object shared causes unexpected mutations [OK]Quick Trick: Default objects are shared, use block for unique elements [OK]Common Mistakes:Thinking syntax is invalidBelieving size must be blockIgnoring shared reference issue
Master "Arrays" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Arrays - Compact for removing nil values - Quiz 7medium Control Flow - If, elsif, else statements - Quiz 4medium Hashes - Dig method for nested access - Quiz 13medium Methods - Variable-length arguments (*args) - Quiz 6medium Methods - Implicit return (last expression) - Quiz 8hard Operators and Expressions - Conditional assignment (||=) - Quiz 5medium Ruby Basics and Runtime - How Ruby interprets code at runtime - Quiz 14medium Ruby Basics and Runtime - Running scripts with ruby command - Quiz 8hard Ruby Basics and Runtime - What is Ruby - Quiz 10hard Variables and Data Types - Integer and Float number types - Quiz 1easy