Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Basics and Runtime
What will this Ruby code print?
# Calculate factorial
factorial = 1
(1..3).each do |i|
  factorial *= i # multiply
end
puts factorial
A6
B123
CError: undefined variable
D1
Step-by-Step Solution
Solution:
  1. Step 1: Trace the loop calculation

    factorial starts at 1, then multiplies by 1, 2, and 3 in the loop.
  2. Step 2: Calculate factorial value

    1 * 1 = 1, 1 * 2 = 2, 2 * 3 = 6, so factorial is 6.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Comments ignored, factorial = 6 [OK]
Quick Trick: Comments do not affect calculations [OK]
Common Mistakes:
  • Thinking comments run as code
  • Confusing multiplication with addition
  • Expecting string output from comments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes