ProgrammingVery hardProduct-company MCQ pattern

A loop runs s += c for 100000 single characters in CPython. Which statement is most accurate?

  1. A. It is often fast in CPython because the string can be resized in place when only one reference exists, but this is an implementation detail; ''.join(list) is the guaranteed linear choice
  2. B. It is always O(n) amortised because strings are mutable buffers
  3. C. It is always O(n squared) and always at least 10000 times slower than join
  4. D. It raises MemoryError once the string passes 65536 characters

Correct answer: A. It is often fast in CPython because the string can be resized in place when only one reference exists, but this is an implementation detail; ''.join(list) is the guaranteed linear choice

CPython has an optimisation for s += that sometimes avoids copying, but the language does not guarantee it. Collecting pieces and joining once is linear by design.

More Python Programming questions

Exams that ask this topic