Bird
0
0

What does the following Ruby code do?

easy📝 Conceptual Q11 of 15
Ruby - Control Flow
What does the following Ruby code do?
puts "Hello" if true
APrints "Hello" because the condition is true
BPrints nothing because the condition is false
CRaises a syntax error
DPrints "Hello" only if false
Step-by-Step Solution
Solution:
  1. Step 1: Understand the inline if condition

    The code uses if true, so the condition is always true.
  2. Step 2: Determine the action based on the condition

    Since the condition is true, puts "Hello" runs and prints "Hello".
  3. Final Answer:

    Prints "Hello" because the condition is true -> Option A
  4. Quick Check:

    Inline if with true condition prints output [OK]
Quick Trick: If condition is true, inline if runs the code [OK]
Common Mistakes:
  • Thinking it prints only if condition is false
  • Confusing inline if with inline unless
  • Expecting syntax error for inline if

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes