Courseiva
Strings →mediumMultiple Choice

PCAP Strings Practice Question

Exhibit

Refer to the exhibit.

```
# Error log
Traceback (most recent call last):
  File "server.py", line 45, in <module>
    message = "Welcome " + username.encode('utf-8') + "!"
TypeError: can only concatenate str (not "bytes") to str
```

What is the cause of the error?

⚠ Common exam trap

The PCAP exam often tests the distinction between `str` and `bytes` types in Python, and the trap here is that candidates mistakenly think `encode` returns a string or that the error is about the method call itself, rather than recognizing the type mismatch in concatenation.

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

✓

The result of `username.encode('utf-8')` is bytes, and Python does not allow concatenating str with bytes.

`username.encode('utf-8')` returns a bytes object, and Python's `+` operator does not allow concatenation of `str` and `bytes` types. This raises a `TypeError: can only concatenate str (not "bytes") to str` because the other operand in the concatenation is a string literal.

Answer analysis

Option-by-option breakdown

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

  • ✗

    The error is because `encode` returns a string, but the plus operator expects two strings.

    Why it's wrong here

    This misidentifies the return type of `str.encode()`. In Python 3, `encode()` always produces a `bytes` object, not a `str`. The actual `TypeError` arises because the `+` operator attempts to concatenate a `str` literal with that `bytes` object, and Python deliberately forbids mixing text and binary data in concatenation.

  • ✗

    The `username` variable is of type bytes, so encoding is unnecessary.

    Why it's wrong here

    This assumption is incorrect because the code treats `username` as a string by calling `.encode()` on it; if `username` were truly `bytes`, it would not have an `encode` method—you would use `decode`. Moreover, the error message shows a concatenation mismatch between `str` and `bytes`, not an error about calling `.encode()` on a bytes object. The variable is simply a `str` whose encoded form cannot be directly added to another `str`.

  • ✓

    The result of `username.encode('utf-8')` is bytes, and Python does not allow concatenating str with bytes.

    Why this is correct

    In Python, `str.encode('utf-8')` transforms the text into a sequence of bytes. The `+` operator between a `str` and a `bytes` object is undefined because the two types represent different kinds of data—human-readable text versus raw binary. Python raises a `TypeError` to avoid ambiguous implicit conversions, requiring the programmer to explicitly decode the `bytes` back to `str` or encode the other operand.

  • ✗

    The encode method is called incorrectly; it should be `username.encode()` without argument.

    Why it's wrong here

    The invocation `username.encode('utf-8')` is perfectly valid: `utf-8` is the standard default encoding and one of many accepted parameters. Calling `encode()` with no argument would also default to UTF-8, so the argument is not the source of the problem. The actual issue lies in the type mismatch when concatenating the resulting bytes with a string literal.

About these practice questions

One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.