LearnFrenzy
Sign in Take a free test

Strings and StringBuilder · Java Strings MCQ · Question 10

Why += on Strings in a loop is slow Answer, explanation & discussion

  • MCQ
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

Why is s += x inside a loop slow for large loops?

  • A += is not optimised by the JIT
  • B Each += creates a new String and copies all previous characters, giving O(n²) total work Correct
  • C Strings are stored in files
  • D It uses synchronisation
Correct Answer: Option B

A StringBuilder appends into a growing buffer in amortised O(1) per append.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links