Bird
0
0

Why must parameters with default values be placed after parameters without default values in Ruby method definitions?

hard📝 Conceptual Q10 of 15
Ruby - Methods
Why must parameters with default values be placed after parameters without default values in Ruby method definitions?
ABecause default parameters are evaluated before required parameters.
BBecause Ruby does not allow any parameters without defaults after parameters with defaults.
CBecause Ruby assigns arguments to parameters in order, and defaults allow skipping only trailing arguments.
DBecause Ruby requires all parameters to have default values if any have defaults.
Step-by-Step Solution
Solution:
  1. Step 1: Understand argument assignment order

    Ruby assigns arguments to parameters from left to right in order.
  2. Step 2: Explain why default parameters must be last

    If a parameter with default is before one without, Ruby cannot tell if an argument is for the default or the required parameter, causing ambiguity.
  3. Final Answer:

    Because Ruby assigns arguments to parameters in order, and defaults allow skipping only trailing arguments. -> Option C
  4. Quick Check:

    Parameter order matters for default argument assignment [OK]
Quick Trick: Default parameters must be last to avoid argument ambiguity [OK]
Common Mistakes:
MISTAKES
  • Thinking Ruby allows any parameter order
  • Believing default parameters evaluated first
  • Assuming all parameters must have defaults if any do

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes