← Strings Quiz: all 8 questions
Strings Quiz · Question 1 of 8
Why is s = s + ch inside a loop slow in Java and Python?
Why is s = s + ch inside a loop slow in Java and Python?
- A.Strings can only hold 255 characters
- B.Strings are immutable, so each + copies the whole string
- C.The + operator is not defined for strings
- D.It isn't slow
Answer & explanation
Correct answer: Strings are immutable, so each + copies the whole string
Every concatenation creates a new string and copies the old characters → O(n²) overall. Use StringBuilder / "".join().
Can you get the rest right without peeking?
Take the timed Strings Quiz quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →