Bird
0
0

Which of the following is the correct syntax to include a newline character inside a string in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Variables and Data Types
Which of the following is the correct syntax to include a newline character inside a string in Ruby?
A"Hello nWorld"
B'Hello\nWorld'
C"Hello\\nWorld"
D"Hello\nWorld"
Step-by-Step Solution
Solution:
  1. Step 1: Understand how Ruby handles escape sequences

    Escape sequences like \n (newline) work only inside double-quoted strings, not single-quoted.
  2. Step 2: Identify the correct string with newline

    Double quotes with \n will create a newline, so "Hello\nWorld" is correct.
  3. Final Answer:

    "Hello\nWorld" -> Option D
  4. Quick Check:

    Escape sequences need double quotes [OK]
Quick Trick: Use double quotes for special characters like \n [OK]
Common Mistakes:
  • Using single quotes which treat \n as literal text
  • Forgetting to escape backslash properly
  • Confusing single and double quotes for escape sequences

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes