Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Methods
What will be the output of this Ruby code?
def empty_string?(str)
  str.empty?
end

puts empty_string?("")
puts empty_string?("hello")
Afalse\ntrue
Btrue\ntrue
Ctrue\nfalse
Dfalse\nfalse
Step-by-Step Solution
Solution:
  1. Step 1: Understand the method behavior

    The method empty_string? calls str.empty? which returns true if str is an empty string.
  2. Step 2: Evaluate the calls

    empty_string?("") returns true because "" is empty; empty_string?("hello") returns false because "hello" is not empty.
  3. Final Answer:

    true\nfalse -> Option C
  4. Quick Check:

    Empty string check returns true, non-empty returns false [OK]
Quick Trick: empty? returns true only for empty strings [OK]
Common Mistakes:
  • Assuming non-empty strings return true
  • Confusing empty? with nil? method
  • Ignoring the question mark in method names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes