Bird
0
0

You want to create a greeting message that includes a user's name and the current year calculated dynamically. Which code correctly uses string interpolation to achieve this?

hard📝 Application Q15 of 15
Ruby - Variables and Data Types
You want to create a greeting message that includes a user's name and the current year calculated dynamically. Which code correctly uses string interpolation to achieve this?
name = "Sam"
current_year = Time.now.year
# Choose the correct greeting message
A"Hello, #{name}! Welcome to current_year."
B"Hello, #{name}! Welcome to #{current_year}."
C'Hello, #{name}! Welcome to #{current_year}.'
D"Hello, name! Welcome to #{Time.now.year}."
Step-by-Step Solution
Solution:
  1. Step 1: Check interpolation of variables

    "Hello, #{name}! Welcome to #{current_year}." correctly uses double quotes and interpolates both name and current_year variables.
  2. Step 2: Verify dynamic year calculation

    "Hello, name! Welcome to #{Time.now.year}." uses Time.now.year inside interpolation but misses interpolating name properly (it's plain text). "Hello, #{name}! Welcome to current_year." treats current_year as plain text. 'Hello, #{name}! Welcome to #{current_year}.' uses single quotes, so no interpolation.
  3. Final Answer:

    "Hello, #{name}! Welcome to #{current_year}." -> Option B
  4. Quick Check:

    Double quotes + #{variable} for dynamic values [OK]
Quick Trick: Use double quotes and #{variable} for dynamic strings [OK]
Common Mistakes:
MISTAKES
  • Using single quotes which disable interpolation
  • Not interpolating variables properly
  • Confusing variable names with plain text

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes