Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - String Operations
What will be the output of this Ruby code?
text = "abc"
text << "def"
text << 33.chr
puts text
Aabcdef33
Babcdef!
Cabcdef
DError: can't convert Fixnum to String
Step-by-Step Solution
Solution:
  1. Step 1: Understand 33.chr

    33.chr returns the character with ASCII code 33, which is "!".
  2. Step 2: Append characters to string

    Appending "def" then "!" results in "abcdef!".
  3. Final Answer:

    abcdef! -> Option B
  4. Quick Check:

    Appending characters with << works with strings [OK]
Quick Trick: Use chr to convert ASCII code to character [OK]
Common Mistakes:
  • Expecting 33 to append as string
  • Confusing chr output
  • Thinking << can't append characters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes