0
0
Rubyprogramming~20 mins

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

Choose your learning style9 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.