Bird
Raised Fist0
Rubyprogramming~20 mins

Parameters with default values in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Default Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of method with default parameters
What is the output of this Ruby code?
Ruby
def greet(name = "Guest", greeting = "Hello")
  "#{greeting}, #{name}!"
end

puts greet("Alice")
AHello, Guest!
BAlice, Hello!
CHello, Alice!
DError: wrong number of arguments
Attempts:
2 left
💡 Hint
Check which parameters are given and which use default values.
❓ Predict Output
intermediate
2:00remaining
Method call with all default parameters
What will this Ruby code print?
Ruby
def calculate(a = 5, b = 10)
  a * b
end

puts calculate
A50
B15
CError: wrong number of arguments
D0
Attempts:
2 left
💡 Hint
No arguments are passed, so all default values are used.
🔧 Debug
advanced
2:00remaining
Identify the error in method with default parameters
What error does this Ruby code raise when run?
Ruby
def add(x, y = 2, z)
  x + y + z
end

puts add(1, 3, 4)
ANo error, outputs 8
BSyntaxError: default argument followed by non-default argument
CTypeError: undefined method '+' for nil:NilClass
DArgumentError: wrong number of arguments (given 3, expected 2)
Attempts:
2 left
💡 Hint
In Ruby, parameters with default values must come after parameters without defaults.
❓ Predict Output
advanced
2:00remaining
Output with mixed default and explicit parameters
What is the output of this Ruby code?
Ruby
def info(name = "Unknown", age = 0, city = "Nowhere")
  "#{name} is #{age} years old and lives in #{city}."
end

puts info("Bob", city: "Paris")
ABob is city: Paris years old and lives in Nowhere.
BBob is 0 years old and lives in Nowhere.
CBob is 0 years old and lives in Paris.
DError: wrong number of arguments
Attempts:
2 left
💡 Hint
Check how Ruby handles keyword arguments versus positional arguments.
🧠 Conceptual
expert
2:00remaining
Number of parameters in method with defaults and splat
Given this Ruby method, how many arguments can it accept without error?
Ruby
def example(a, b = 2, *c, d)
  [a, b, c, d]
end
AAt least 2 arguments, no maximum limit
BExactly 4 arguments only
CAt least 3 arguments, no maximum limit
DAt least 1 argument, no maximum limit
Attempts:
2 left
💡 Hint
Consider required, optional, splat, and default parameters in Ruby.

Practice

(1/5)
1. What does a default parameter value in a Ruby method do?
def greet(name = "Guest")
easy
A. It causes a syntax error if the argument is missing.
B. It makes the parameter mandatory every time the method is called.
C. It changes the method name automatically.
D. It allows the method to be called without that argument, using the default instead.

Solution

  1. Step 1: Understand default parameter purpose

    Default parameters provide a fallback value if no argument is given.
  2. Step 2: Apply to the example method

    In def greet(name = "Guest"), if no name is passed, "Guest" is used.
  3. Final Answer:

    It allows the method to be called without that argument, using the default instead. -> Option D
  4. Quick Check:

    Default parameter = fallback value [OK]
Hint: Default means optional argument with fallback value [OK]
Common Mistakes:
  • Thinking default parameters are mandatory
  • Assuming missing arguments cause errors
  • Confusing default values with method overloading
2. Which of the following is the correct syntax to define a method with a default parameter in Ruby?
easy
A. def add(a, b = 5); a + b; end
B. def add(a =, b 5); a + b; end
C. def add(a, b: 5); a + b; end
D. def add(a, b); b = 5; a + b; end

Solution

  1. Step 1: Review Ruby default parameter syntax

    Default values are set with = value after the parameter name.
  2. Step 2: Check each option

    def add(a, b = 5); a + b; end uses b = 5 correctly. def add(a =, b 5); a + b; end has invalid syntax. def add(a, b: 5); a + b; end uses keyword argument style, not default parameter. def add(a, b); b = 5; a + b; end assigns inside method body, not default parameter.
  3. Final Answer:

    def add(a, b = 5); a + b; end -> Option A
  4. Quick Check:

    Default param syntax = param = value [OK]
Hint: Use = after parameter name for default values [OK]
Common Mistakes:
  • Using colon instead of equals for default values
  • Placing = before parameter name
  • Assigning default inside method body instead of parameter list
3. What is the output of this Ruby code?
def multiply(x, y = 2)
  x * y
end

puts multiply(4)
puts multiply(4, 3)
medium
A. 8\n12
B. 4\n7
C. Error: missing argument
D. 6\n12

Solution

  1. Step 1: Analyze method calls with default parameter

    First call: multiply(4) uses default y=2, so 4*2=8. Second call: multiply(4,3) uses y=3, so 4*3=12.
  2. Step 2: Determine output of puts statements

    Outputs are printed on separate lines: 8 then 12.
  3. Final Answer:

    8 12 -> Option A
  4. Quick Check:

    Default param used when missing = 8, explicit param = 12 [OK]
Hint: Default used if argument missing, else given value [OK]
Common Mistakes:
  • Expecting error when argument is missing
  • Confusing default parameter with keyword arguments
  • Mixing up output values
4. Identify the error in this Ruby method definition:
def greet(name = "Guest", age)
  "Hello #{name}, you are #{age} years old."
end
medium
A. Method name cannot be 'greet' with parameters.
B. Missing return statement causes error.
C. Default parameters must come after non-default parameters.
D. String interpolation syntax is incorrect.

Solution

  1. Step 1: Check parameter order rules in Ruby

    Parameters with default values must be placed after parameters without defaults.
  2. Step 2: Analyze given method signature

    Here, name has default, but age does not and comes after. This causes syntax error.
  3. Final Answer:

    Default parameters must come after non-default parameters. -> Option C
  4. Quick Check:

    Default params after required params [OK]
Hint: Put default params after required ones [OK]
Common Mistakes:
  • Placing default parameters before required ones
  • Assuming return statement is mandatory
  • Misreading string interpolation syntax
5. You want to define a method order that takes a product name and an optional quantity with default 1. Which is the best way to define it to allow calling order("apple") and order("apple", 5)?
hard
A. def order(product = "apple", quantity); "Order #{quantity} #{product}(s)"; end
B. def order(product, quantity = 1); "Order #{quantity} #{product}(s)"; end
C. def order(quantity = 1, product); "Order #{quantity} #{product}(s)"; end
D. def order(product, quantity); quantity ||= 1; "Order #{quantity} #{product}(s)"; end

Solution

  1. Step 1: Identify correct parameter order and defaults

    Required parameters come first, then optional with default values.
  2. Step 2: Evaluate each option

    def order(product, quantity = 1); "Order #{quantity} #{product}(s)"; end correctly places product first and quantity with default 1 second. def order(product = "apple", quantity); "Order #{quantity} #{product}(s)"; end has default before required, invalid. def order(quantity = 1, product); "Order #{quantity} #{product}(s)"; end swaps order incorrectly. def order(product, quantity); quantity ||= 1; "Order #{quantity} #{product}(s)"; end assigns default inside method but requires both arguments.
  3. Final Answer:

    def order(product, quantity = 1); "Order #{quantity} #{product}(s)"; end -> Option B
  4. Quick Check:

    Required first, default second = def order(product, quantity = 1); "Order #{quantity} #{product}(s)"; end [OK]
Hint: Put required params first, defaults after [OK]
Common Mistakes:
  • Placing default parameter before required
  • Assigning default inside method body instead of parameter list
  • Confusing parameter order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard