Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Methods
What is the output of this Ruby code?
def greet(name = "Guest")
  "Hello, #{name}!"
end

puts greet
puts greet("Alice")
AHello, Guest! Hello, Alice!
BHello, ! Hello, Alice!
CHello, Guest! Hello, Guest!
DError: missing argument
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method calls

    Calling greet without argument uses default "Guest". Calling greet("Alice") uses "Alice".
  2. Step 2: Determine output strings

    First call outputs "Hello, Guest!". Second call outputs "Hello, Alice!".
  3. Final Answer:

    Hello, Guest!\nHello, Alice! -> Option A
  4. Quick Check:

    Default parameter used when argument missing [OK]
Quick Trick: Default parameter fills missing argument in call [OK]
Common Mistakes:
MISTAKES
  • Expecting error when argument is missing
  • Assuming default parameter is ignored
  • Confusing output strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes