Bird
0
0

You want to print all even numbers from 2 to 10 using an until loop in Ruby. Which code snippet correctly does this?

hard📝 Application Q8 of 15
Ruby - Loops and Iteration
You want to print all even numbers from 2 to 10 using an until loop in Ruby. Which code snippet correctly does this?
Anum = 2 until num >= 10 do puts num num += 2 end
Bnum = 2 until num > 10 do puts num num += 2 end
Cnum = 2 until num < 10 do puts num num += 2 end
Dnum = 2 until num == 10 do puts num num += 2 end
Step-by-Step Solution
Solution:
  1. Step 1: Understand the loop condition

    We want to print even numbers up to 10, including 10, so loop should run until num > 10.
  2. Step 2: Check increments and output

    Starting at 2, incrementing by 2 each time prints 2,4,6,8,10 correctly.
  3. Final Answer:

    num = 2 until num > 10 do puts num num += 2 end -> Option B
  4. Quick Check:

    Until loop condition and increment = A [OK]
Quick Trick: Use until num > max for inclusive upper bound [OK]
Common Mistakes:
  • Using wrong comparison operator
  • Missing last number in output
  • Incrementing by wrong step

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes