Question 53 of 511
StringsmediumMultiple ChoiceObjective-mapped

Quick Answer

The correct answer is both A and B, as each resolves the string concatenation type error by ensuring the integer is treated as a string before the plus operator is applied. In Python, the `+` operator only concatenates two strings; attempting to combine a string with an integer raises a TypeError because the interpreter cannot implicitly convert the integer to a string. Option A directly concatenates the string literal `'5'`, while Option B explicitly converts the integer `5` using `str()`, both producing `'Hello5'` without error. On the Certified Associate Python Programmer PCAP exam, this question tests your understanding of type coercion and the strictness of Python’s dynamic typing—a common trap is assuming Python will auto-convert integers to strings during concatenation. Remember the memory tip: “Strings stick to strings; numbers need a `str()` ring.”

PCAP Strings Practice Question

This PCAP practice question tests your understanding of strings. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

Exhibit

Error log:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    print('Hello' + 5)
TypeError: can only concatenate str (not "int") to str

Refer to the exhibit. Which of the following fixes the error?

Question 1mediummultiple choice
Full question →

Exhibit

Error log:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    print('Hello' + 5)
TypeError: can only concatenate str (not "int") to str

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

Both A and B

Option C is correct because both A and B produce the string 'Hello5' without error. In Python, the + operator concatenates strings, so 'Hello' + '5' works. Option B converts the integer 5 to a string using str() before concatenation, which also works. Option D uses the * operator to repeat the string 'Hello' five times, producing 'HelloHelloHelloHelloHello', which is a valid operation but does not fix the error described in the exhibit (likely a TypeError from trying to concatenate a string and an integer).

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Answer analysis

Option-by-option breakdown

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

  • print('Hello' + '5')

    Why it's wrong here

    This works, but option D is also correct because A also works.

  • print('Hello' + str(5))

    Why it's wrong here

    This works, but option D is also correct because B also works.

  • Both A and B

    Why this is correct

    Both convert the integer to a string before concatenation.

    Related concept

    Read the scenario before looking for a memorised answer.

  • print('Hello' * 5)

    Why it's wrong here

    This concatenates 'Hello' 5 times, not adding 5.

Common exam traps

Common exam trap: answer the scenario, not the keyword

Python Institute often tests the distinction between implicit type conversion (which Python does not do for string+int) and explicit conversion using str(), and candidates may forget that string repetition with * is valid but does not solve a concatenation error.

Detailed technical explanation

How to think about this question

In Python, the + operator is overloaded: for strings it performs concatenation, for numbers it performs addition. Attempting to use + between a string and an integer raises a TypeError because Python does not implicitly convert types. The str() function explicitly converts an integer to its string representation, allowing concatenation. The * operator for strings performs repetition, returning a new string repeated n times, which is a different operation entirely.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A cloud solutions architect for a retail company is evaluating services for a new workload. The correct answer here reflects best practice for the specific scenario described — not a general cloud recommendation. Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option. Cloud exam questions reward reading the constraint carefully: the same technology can be right or wrong depending on the use case.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related PCAP practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free PCAP practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this PCAP question test?

Strings — This question tests Strings — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: Both A and B — Option C is correct because both A and B produce the string 'Hello5' without error. In Python, the + operator concatenates strings, so 'Hello' + '5' works. Option B converts the integer 5 to a string using str() before concatenation, which also works. Option D uses the * operator to repeat the string 'Hello' five times, producing 'HelloHelloHelloHelloHello', which is a valid operation but does not fix the error described in the exhibit (likely a TypeError from trying to concatenate a string and an integer).

What should I do if I get this PCAP question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more ways this is tested on PCAP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. Refer to the exhibit. What is the output?

medium
  • A.IndexError
  • B.No output
  • C.Hello World
  • D.TypeError

Why D: Option D is correct because the code attempts to concatenate a string ('Hello ') with an integer (123) using the '+' operator. In Python, this raises a TypeError, as the '+' operator for strings expects both operands to be strings. The error message would be 'TypeError: can only concatenate str (not "int") to str'.

Keep practising

More PCAP practice questions

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.