ProgrammingVery hardProduct-company MCQ pattern
A loop runs s += c for 100000 single characters in CPython. Which statement is most accurate?
- 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
- B. It is always O(n) amortised because strings are mutable buffers
- C. It is always O(n squared) and always at least 10000 times slower than join
- 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
- Which statement about converting a string to an int in Python 3.13 is correct?
- What relative and absolute tolerances does math.isclose use by default?
- Which key sorts records by score descending and then by name ascending, using one stable sort?
- What is the largest float that is less than 1.0 in Python, as printed by repr?
- What is printed? python print(3 // 2 2 + 3 % 2, -3 // 2 2 + -3 % 2)
- What is printed? python print('%s and %r' % ('a', 'a'))