Bird
0
0

What will this Ruby code output?

medium📝 Predict Output Q5 of 15
Ruby - Blocks, Procs, and Lambdas

What will this Ruby code output?

words = ['cat', 'dog']
words.each { |w| puts w.upcase }
ACAT DOG
BError: undefined method upcase
C['CAT', 'DOG']
Dcat dog
Step-by-Step Solution
Solution:
  1. Step 1: Identify block parameter and method

    Each word w is converted to uppercase using upcase method inside the block.
  2. Step 2: Determine output

    Outputs are 'CAT' and 'DOG' printed on separate lines.
  3. Final Answer:

    CAT DOG -> Option A
  4. Quick Check:

    Block parameter transformed with upcase = CAT, DOG [OK]
Quick Trick: Use block parameter to call methods on each element [OK]
Common Mistakes:
  • Printing original lowercase words
  • Expecting array output instead of printed lines
  • Mistaking method name or syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes