Bird
0
0

Which statement best explains why StringBuilder is not thread-safe while StringBuffer is?

hard📝 Conceptual Q10 of 15
Java - Strings and String Handling
Which statement best explains why StringBuilder is not thread-safe while StringBuffer is?
A<code>StringBuilder</code> methods are synchronized; <code>StringBuffer</code> methods are not.
BBoth are thread-safe because they are mutable.
C<code>StringBuffer</code> methods are synchronized; <code>StringBuilder</code> methods are not.
DNeither is thread-safe because they both modify strings.
Step-by-Step Solution
Solution:
  1. Step 1: Understand synchronization in StringBuffer

    StringBuffer methods are synchronized, making it safe for use by multiple threads.
  2. Step 2: Understand lack of synchronization in StringBuilder

    StringBuilder methods are not synchronized, so it is faster but not thread-safe.
  3. Final Answer:

    StringBuffer methods are synchronized; StringBuilder methods are not. -> Option C
  4. Quick Check:

    Synchronization = StringBuffer; no sync = StringBuilder [OK]
Quick Trick: StringBuffer is synchronized; StringBuilder is faster but not thread-safe [OK]
Common Mistakes:
  • Confusing which class is synchronized
  • Assuming both are thread-safe

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes