Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Control Flow
What will be the output of the following Ruby code?
def check_age(age)
  return "Too young" if age < 18
  "Welcome"
end

puts check_age(16)
puts check_age(20)
AWelcome\nWelcome
BWelcome\nToo young
CToo young\nToo young
DToo young\nWelcome
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method behavior for age 16

    Since 16 < 18, the guard clause triggers and returns "Too young" immediately.
  2. Step 2: Analyze method behavior for age 20

    Since 20 >= 18, guard clause is skipped and method returns "Welcome".
  3. Final Answer:

    Too young\nWelcome -> Option D
  4. Quick Check:

    Guard clause triggers only if age < 18 [OK]
Quick Trick: Guard clause returns early if condition true [OK]
Common Mistakes:
  • Assuming method always returns 'Welcome'
  • Mixing up output order
  • Ignoring guard clause effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes