Bird
0
0

Which of the following is the best way to add "Ruby" to the string variable lang without creating a new string?

easy📝 Conceptual Q2 of 15
Ruby - String Operations
Which of the following is the best way to add "Ruby" to the string variable lang without creating a new string?
Alang = lang + "Ruby"
Blang.append("Ruby")
Clang += "Ruby"
Dlang << "Ruby"
Step-by-Step Solution
Solution:
  1. Step 1: Identify methods that modify the original string

    The << operator appends to the original string without creating a new one.
  2. Step 2: Compare options

    lang = lang + "Ruby" creates a new string, lang += "Ruby" also creates a new string, lang.append("Ruby") does not exist. << is the simplest and most common for this purpose.
  3. Final Answer:

    lang << "Ruby" -> Option D
  4. Quick Check:

    Use << to append in place [OK]
Quick Trick: << appends directly, no new string made [OK]
Common Mistakes:
  • Using + which creates new string
  • Using += which creates new string
  • Assuming append is a Ruby method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes