Courseiva
Strings →hardMultiple Choice

PCAP Strings Practice Question

A developer is building a large string by concatenating many substrings in a loop using '+'. What is the main performance issue?

⚠ Common exam trap

Python Institute often tests the misconception that string immutability means concatenation is impossible or illegal, when in fact the real issue is the hidden performance cost of repeated object creation in loops.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

Each concatenation creates a new string object, leading to quadratic time complexity

In Python, strings are immutable, so the '+' operator does not modify an existing string but creates a new string object each time it is used. In a loop, this results in O(n²) time complexity because each concatenation copies the entire accumulated string, making it highly inefficient for large or many substrings.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    Each concatenation creates a new string object, leading to quadratic time complexity

    Why this is correct

    Strings in Python are immutable, and the `+` operator cannot modify an existing string; it must allocate a brand-new string object and copy the contents of both operands into it. When you concatenate in a loop, each iteration copies all previously accumulated characters plus the new piece, so total work grows quadratically (O(n^2)) as the string length increases. Using a list and `str.join()` avoids this repeated copying.

  • ✗

    String concatenation is not allowed in loops

    Why it's wrong here

    This statement is false: Python fully supports the `+` operator inside loops, so code like `s = s + part` compiles and runs without any syntax or runtime error. The real issue is performance, not legality — each loop iteration still triggers the same immutable-string allocation and copying described above. A loop is allowed, but it is the classic anti-pattern that leads to quadratic time, not a language restriction.

  • ✗

    Strings are immutable, so concatenation is impossible

    Why it's wrong here

    This confuses immutability with impossibility. In Python, an immutable object cannot be changed in place, so you cannot modify the bytes of an existing string; however, you can always create a new string and rebind the variable. Concatenation remains fully supported, it merely produces a fresh string object rather than mutating the old one. Thus immutability prohibits in-place mutation, not concatenation.

  • ✗

    The '+' operator works only for characters, not strings

    Why it's wrong here

    The `+` operator is overloaded for the `str` type and concatenates entire strings of any length, not just single characters. For example, `'ab' + 'cd'` yields `'abcd'`, and even a single character is simply a string of length one, so the distinction is meaningless. It also works with string literals and variables alike, making it a general string concatenation operator.

About these practice questions

This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.