0
0
Rubyprogramming~20 mins

String concatenation and << in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby String Concatenation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby code?
Consider the following Ruby code snippet. What will be printed when it runs?
Ruby
str = "Hello"
str << " World"
puts str
AHelloWorld
BHello World\n
CHello World
DHello << World
Attempts:
2 left
💡 Hint
The << operator appends the string on the right to the string on the left with a space if included.
Predict Output
intermediate
2:00remaining
What does this code print?
What will be the output of this Ruby code?
Ruby
a = "foo"
b = a
b << "bar"
puts a
Afoobar
Bfoo
Cbar
Dfoobarbar
Attempts:
2 left
💡 Hint
Remember that << modifies the original string in place.
🔧 Debug
advanced
2:00remaining
Why does this code raise an error?
This Ruby code raises an error. What is the cause?
Ruby
str = nil
str << "test"
ANoMethodError because nil does not have << method
BRuntimeError because string is empty
CTypeError because << expects a number
DSyntaxError due to missing quotes
Attempts:
2 left
💡 Hint
Check what methods nil supports.
🧠 Conceptual
advanced
2:00remaining
What is the difference between + and << for strings in Ruby?
Choose the correct statement about the difference between + and << when used with strings in Ruby.
A+ and << both modify the original string in place
B+ modifies the original string, << creates a new string object
C+ and << both create new string objects
D+ creates a new string object, << modifies the original string in place
Attempts:
2 left
💡 Hint
Think about whether the original string changes after using + or <<.
Predict Output
expert
3:00remaining
What is the final output of this Ruby code?
Analyze the following Ruby code and determine what it prints.
Ruby
str1 = "abc"
str2 = str1
str1 += "def"
str2 << "xyz"
puts str1
puts str2
Aabcdef\nabcdef
Babcdef\nabcxyz
Cabcxyz\nabcxyz
Dabcdefxyz\nabcdefxyz
Attempts:
2 left
💡 Hint
Remember that += creates a new string object, while << modifies the original.