Bird
Raised Fist0
Rubyprogramming~20 mins

String creation (single and double quotes) in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby String Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of single vs double quoted strings
What is the output of this Ruby code snippet?
Ruby
puts 'Hello\nWorld'
puts "Hello\nWorld"
A
Hello\nWorld
Hello
World
B
Hello
World
Hello
World
C
Hello
World
Hello\nWorld
DHello\nWorld\nHello\nWorld
Attempts:
2 left
💡 Hint
Remember how single and double quotes treat escape sequences differently in Ruby.
❓ Predict Output
intermediate
2:00remaining
String interpolation behavior
What will this Ruby code output?
Ruby
name = 'Alice'
puts 'Hello, #{name}!'
puts "Hello, #{name}!"
A
Hello, Alice!
Hello, Alice!
B
Hello, #{name}!
Hello, Alice!
C
Hello, #{name}!
Hello, #{name}!
D
Hello, Alice!
Hello, #{name}!
Attempts:
2 left
💡 Hint
Check how Ruby treats #{...} inside single vs double quotes.
🔧 Debug
advanced
2:00remaining
Identify the syntax error in string creation
Which option will cause a syntax error when running this Ruby code?
Ruby
str = 'It's a sunny day'
Astr = 'It\'s a sunny day'
Bstr = "It's a sunny day"
Cstr = %q{It's a sunny day}
Dstr = 'It's a sunny day'
Attempts:
2 left
💡 Hint
Look carefully at how single quotes inside single quoted strings are handled.
🧠 Conceptual
advanced
2:00remaining
Difference in escape sequence handling
Which statement best describes how Ruby treats escape sequences in single and double quoted strings?
ADouble quoted strings interpret escape sequences like \n and \t, single quoted do not except for \\ and \'.
BSingle quoted strings interpret escape sequences like \n and \t, double quoted do not.
CBoth single and double quoted strings interpret all escape sequences the same way.
DNeither single nor double quoted strings interpret escape sequences.
Attempts:
2 left
💡 Hint
Think about which escape sequences work in single quoted strings.
❓ Predict Output
expert
3:00remaining
Complex string with mixed quotes and interpolation
What is the output of this Ruby code?
Ruby
value = 10
str = 'Value is #{value}'
str2 = "Value is #{value}"
str3 = %q{Value is #{value}}
str4 = %Q{Value is #{value}}
puts str
puts str2
puts str3
puts str4
A
Value is 10
Value is 10
Value is 10
Value is 10
B
Value is #{value}
Value is #{value}
Value is #{value}
Value is #{value}
C
Value is #{value}
Value is 10
Value is #{value}
Value is 10
D
Value is 10
Value is #{value}
Value is 10
Value is #{value}
Attempts:
2 left
💡 Hint
Remember how %q and %Q behave like single and double quotes respectively.

Practice

(1/5)
1. Which of the following is the correct way to create a simple string with the text Hello World in Ruby?
easy
A. Using single quotes: 'Hello World'
B. Using double quotes: "Hello World"
C. Using backticks: `Hello World`
D. Using parentheses: (Hello World)

Solution

  1. Step 1: Understand string creation in Ruby

    Ruby allows strings to be created using single or double quotes. Single quotes are simplest for plain text.
  2. Step 2: Identify the correct syntax for simple strings

    Single quotes 'Hello World' correctly create a string without special processing.
  3. Final Answer:

    Using single quotes: 'Hello World' -> Option A
  4. Quick Check:

    Simple text string = single quotes [OK]
Hint: Use single quotes for plain text strings [OK]
Common Mistakes:
  • Using backticks which execute commands
  • Using parentheses which are not string delimiters
  • Confusing single and double quotes for simple strings
2. Which of the following is the correct syntax to include a newline character inside a string in Ruby?
easy
A. "Hello nWorld"
B. 'Hello\nWorld'
C. "Hello\\nWorld"
D. "Hello\nWorld"

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]
Hint: 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
3. What will be the output of the following Ruby code?
name = "Alice"
puts 'Hello, #{name}!'
puts "Hello, #{name}!"
medium
A. Hello, #{name}! Hello, Alice!
B. Hello, Alice! Hello, Alice!
C. Hello, #{name}! Hello, #{name}!
D. Hello, Alice! Hello, #{name}!

Solution

  1. Step 1: Understand string interpolation in Ruby

    Interpolation #{variable} works only inside double-quoted strings, not single-quoted.
  2. Step 2: Analyze each puts statement output

    First puts uses single quotes, so prints literally 'Hello, #{name}!'. Second uses double quotes, so inserts variable value 'Alice'.
  3. Final Answer:

    Hello, #{name}! Hello, Alice! -> Option A
  4. Quick Check:

    Interpolation only in double quotes [OK]
Hint: Only double quotes allow #{variable} interpolation [OK]
Common Mistakes:
  • Assuming interpolation works in single quotes
  • Confusing output order
  • Ignoring difference between single and double quotes
4. Identify the error in the following Ruby code:
str = 'It's a sunny day'
medium
A. SyntaxError due to missing double quotes
B. No error, string is valid
C. SyntaxError due to unescaped single quote inside single-quoted string
D. Runtime error due to invalid string

Solution

  1. Step 1: Check string delimiters and content

    The string uses single quotes outside and contains an unescaped single quote inside (It's), which breaks syntax.
  2. Step 2: Understand how to fix the error

    To fix, either escape the inner single quote as \' or use double quotes outside.
  3. Final Answer:

    SyntaxError due to unescaped single quote inside single-quoted string -> Option C
  4. Quick Check:

    Escape inner quotes or use double quotes [OK]
Hint: Escape inner quotes or use double quotes [OK]
Common Mistakes:
  • Ignoring the need to escape inner single quotes
  • Assuming single quotes allow unescaped inner single quotes
  • Confusing syntax and runtime errors
5. You want to create a string that contains both single and double quotes exactly as: She said, "It's fine." Which of the following Ruby strings correctly represents this?
hard
A. 'She said, 'It\'s fine.''
B. "She said, \"It's fine.\""
C. "She said, "It's fine.""
D. 'She said, "It\'s fine."'

Solution

  1. Step 1: Analyze the string content

    The string contains double quotes around the phrase and a single quote inside the word It's.
  2. Step 2: Check each option for correct escaping

    "She said, \"It's fine.\"" uses double quotes outside, escapes inner double quotes with \", and leaves single quote unescaped (allowed inside double quotes). This is correct.
  3. Step 3: Identify errors in other options

    'She said, "It\'s fine."' uses single quotes outside but escapes single quote inside, which is valid but escapes double quotes unnecessarily. "She said, "It's fine."" has unescaped double quotes inside double quotes causing syntax error. 'She said, 'It\'s fine.'' has unescaped single quotes inside single quotes causing syntax error.
  4. Final Answer:

    "She said, \"It's fine.\"" -> Option B
  5. Quick Check:

    Escape inner double quotes inside double quotes [OK]
Hint: Use double quotes outside, escape inner double quotes [OK]
Common Mistakes:
  • Not escaping inner double quotes inside double-quoted strings
  • Using single quotes outside with unescaped inner single quotes
  • Confusing which quotes to escape
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard