Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Loops and Iteration
What is the output of this Ruby code?
fruits = ['apple', 'banana', 'cherry']
fruits.each { |fruit| puts fruit.upcase }
AAPPLE BANANA CHERRY
Bapple banana cherry
C['APPLE', 'BANANA', 'CHERRY']
DError: undefined method upcase for fruit
Step-by-Step Solution
Solution:
  1. Step 1: Understand what each does with puts fruit.upcase

    The each method runs the block for each fruit, printing the uppercase version.
  2. Step 2: Check output line by line

    It prints 'APPLE', then 'BANANA', then 'CHERRY' each on a new line.
  3. Final Answer:

    APPLE BANANA CHERRY -> Option A
  4. Quick Check:

    each prints uppercase fruits [OK]
Quick Trick: Remember puts prints each item on a new line [OK]
Common Mistakes:
  • Thinking each returns a new array
  • Expecting lowercase output
  • Confusing puts with returning a list

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes