0
0
Rubyprogramming~20 mins

Keyword arguments in Ruby - Practice Problems & Coding Challenges

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

puts greet(name: "Alice")
AHello, Alice!
BHello, !
Cnil
DArgumentError
Attempts:
2 left
💡 Hint
Check the default value of the greeting keyword argument.
Predict Output
intermediate
2:00remaining
Keyword argument error
What error does this Ruby code produce?
Ruby
def add(x:, y:)
  x + y
end

add(1, 2)
ATypeError
BArgumentError
CNoMethodError
D0
Attempts:
2 left
💡 Hint
Check how the method expects arguments to be passed.
🔧 Debug
advanced
2:00remaining
Fix the keyword argument usage
This code raises an error. Which option fixes it so it prints "Sum is 7"?
Ruby
def sum_numbers(a:, b:)
  puts "Sum is #{a + b}"
end

sum_numbers(3, 4)
Asum_numbers(a: 3, b: 4)
Bsum_numbers(3, b: 4)
Csum_numbers(a = 3, b = 4)
Dsum_numbers({a: 3, b: 4})
Attempts:
2 left
💡 Hint
Keyword arguments must be passed with their names.
Predict Output
advanced
2:00remaining
Keyword arguments with double splat operator
What is the output of this Ruby code?
Ruby
def details(**info)
  info.keys.sort.join(",")
end

puts details(age: 30, name: "Bob", city: "NY")
AArgumentError
Bname,age,city
Ccity,name,age
Dage,city,name
Attempts:
2 left
💡 Hint
The method collects all keyword arguments into a hash and sorts keys alphabetically.
🧠 Conceptual
expert
2:00remaining
Behavior of keyword arguments with defaults and overrides
Given this method:
def configure(timeout: 5, retries: 3)
  "Timeout: #{timeout}, Retries: #{retries}"
end

What is the output of configure(timeout: 10)?
ATimeout: 10, Retries: nil
BTimeout: 5, Retries: 3
CTimeout: 10, Retries: 3
DArgumentError
Attempts:
2 left
💡 Hint
Only the timeout argument is overridden; retries uses default.