Bird
0
0

You want to build a thread-safe string that multiple threads can modify safely. Which class and method usage is correct?

hard📝 Application Q8 of 15
Java - Strings and String Handling
You want to build a thread-safe string that multiple threads can modify safely. Which class and method usage is correct?
AUse <code>StringBuffer</code> and its <code>append()</code> method.
BUse <code>StringBuilder</code> and its <code>append()</code> method.
CUse <code>String</code> and '+' operator.
DUse <code>StringBuffer</code> and <code>concat()</code> method.
Step-by-Step Solution
Solution:
  1. Step 1: Identify thread-safe class

    StringBuffer is synchronized and thread-safe.
  2. Step 2: Use append() to modify string

    append() is the correct method to add text.
  3. Final Answer:

    Use StringBuffer and its append() method. -> Option A
  4. Quick Check:

    Thread-safe append = Use StringBuffer and its append() method. [OK]
Quick Trick: StringBuffer is synchronized; use append() to add text [OK]
Common Mistakes:
  • Using StringBuilder for thread safety
  • Using concat() which is not in StringBuffer
  • Using '+' operator for thread safety

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes