0
0
Rubyprogramming~20 mins

String interpolation with #{} in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of string interpolation with variables
What is the output of this Ruby code?
Ruby
name = "Alice"
age = 30
puts "My name is #{name} and I am #{age} years old."
AMy name is Alice and I am 30 years old.
BMy name is Alice and I am #{age} years old.
CMy name is name and I am age years old.
DMy name is #{name} and I am #{age} years old.
Attempts:
2 left
💡 Hint
Remember that #{variable} inside double quotes evaluates the variable's value.
Predict Output
intermediate
2:00remaining
Interpolation with expressions inside #{}
What will this Ruby code print?
Ruby
a = 5
b = 3
puts "Sum is #{a + b} and product is #{a * b}."
ASum is #{a + b} and product is #{a * b}.
BSum is a + b and product is a * b.
CSum is 8 and product is 15.
DSum is 53 and product is 15.
Attempts:
2 left
💡 Hint
Inside #{}, Ruby evaluates the expression before converting to string.
🔧 Debug
advanced
2:00remaining
Identify the error in string interpolation
What error does this Ruby code produce?
Ruby
name = "Bob"
puts 'Hello, #{name}!'
AHello, #{name}!
BHello, Bob!
CSyntaxError
DNameError
Attempts:
2 left
💡 Hint
Check the type of quotes used for string interpolation.
Predict Output
advanced
2:00remaining
Complex interpolation with method call
What is the output of this Ruby code?
Ruby
def greet(name)
  "Hello, #{name.upcase}!"
end
puts greet("eve")
AHello, #{name.upcase}!
BHello, eve!
CNoMethodError
DHello, EVE!
Attempts:
2 left
💡 Hint
Inside #{}, you can call methods on variables.
🧠 Conceptual
expert
3:00remaining
Why does this interpolation fail?
Consider this Ruby code: ```ruby number = 10 puts "Value: #{number + '5'}" ``` What error will this code raise and why?
ASyntaxError due to invalid interpolation syntax
BTypeError because you cannot add Integer and String
CNameError because 'number' is undefined
DNo error, outputs: Value: 105
Attempts:
2 left
💡 Hint
Check the types of operands inside the interpolation expression.