Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Variables and Data Types
What will this Ruby code print?
puts 'Line1\nLine2'
puts "Line1\nLine2"
ALine1 Line2 Line1 Line2
BLine1 Line2 Line1\nLine2
CLine1\nLine2 Line1 Line2
DLine1\nLine2 Line1\nLine2
Step-by-Step Solution
Solution:
  1. Step 1: Understand escape sequences in single-quoted strings

    In single quotes, \n is treated as two characters, not a newline.
  2. Step 2: Understand escape sequences in double-quoted strings

    In double quotes, \n is interpreted as a newline character.
  3. Final Answer:

    Line1\nLine2 Line1 Line2 -> Option C
  4. Quick Check:

    Escape sequences work only in double quotes [OK]
Quick Trick: Escape sequences like \n work only in double quotes [OK]
Common Mistakes:
MISTAKES
  • Expecting \n to create newline in single quotes
  • Confusing escape behavior
  • Mixing output lines

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes