Courseiva

PCEP Practice Question: Data Types, Variables, Basic I/O and Operators

What is the output of the following code? ```python a = 'abc' b = a b = b + 'd'

print(a)

```

⚠ Common exam trap

It's easy for candidates to confuse variable assignment with mutable object behavior, assuming that `b = a` creates a reference that will reflect changes made to `b`, but strings are immutable, so reassignment creates a new object without affecting the original.

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

✓

abc

Strings in Python are immutable. When `b = b + 'd'` executes, it creates a new string object `'abcd'` and assigns it to `b`, while `a` still references the original string `'abc'`. Thus, `print(a)` outputs `abc`.

Answer analysis

Option-by-option breakdown

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

  • ✗

    abc d

    Why it's wrong here

    The concatenation creates 'abcd' with no space, and a still references the original 'abc' object. It is tempting because the printed variable appears to track b's changes, and it would be correct only if a itself were reassigned after the concatenation.

  • ✓

    abc

    Why this is correct

    Strings in Python are immutable, so `b = b + 'd'` creates a new string object rather than modifying the original. The name `b` is rebound to `'abcd'`, while `a` still references `'abc'`. Printing `a` therefore outputs `abc`, satisfying the stem's request for the code's output.

  • ✗

    Error

    Why it's wrong here

    Concatenating two strings with + is valid, so no exception is raised and 'abc' prints. It is tempting because mixing types in + does raise TypeError, and it would be correct if b held an integer rather than a string.

  • ✗

    abcd

    Why it's wrong here

    Strings are immutable, so b = b + 'd' rebinds b to a new object while a still references 'abc'. The concatenation result is tempting because beginners track the variable name rather than the object, and 'abcd' would print only if the code instead executed a = a + 'd'.

About these practice questions

Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on PCEP

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. What does the following code output? try: x = int('abc') except ValueError: print('Invalid')

easy
  • A.The program crashes
  • ✓ B.Invalid
  • C.(Nothing printed)
  • D.abc

Why B: The code attempts to convert the string 'abc' to an integer using int(). Since 'abc' is not a valid integer, Python raises a ValueError. The except block catches this specific exception and executes print('Invalid'), so the output is 'Invalid'. Option B is correct because the exception is handled gracefully without crashing.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCEP 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 PCEP exam.