Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Control Flow

What will be the output of this Ruby code?

num = 15
if num > 20
  puts "Greater than 20"
elsif num > 10
  puts "Greater than 10"
else
  puts "10 or less"
end
AGreater than 20
BGreater than 10
C10 or less
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the first condition

    Check if 15 > 20: false, so skip first block.
  2. Step 2: Evaluate the elsif condition

    Check if 15 > 10: true, so print "Greater than 10".
  3. Final Answer:

    Greater than 10 -> Option B
  4. Quick Check:

    elsif condition true, prints that block = A [OK]
Quick Trick: Conditions checked top to bottom; first true runs [OK]
Common Mistakes:
  • Choosing first if block incorrectly
  • Ignoring elsif block
  • Assuming else runs always

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes