Courseiva

PCEP Computer Programming and Python Fundamentals Practice Question

What is the output of the following code? x = [1, 2, 3]; y = x; y.append(4); print(x)

⚠ Common exam trap

The PCEP exam often tests the distinction between reference assignment and copying for mutable objects, trapping candidates who assume `y = x` creates an independent copy of the list.

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

✓

[1,2,3,4]

In Python, variables hold references to objects, not copies. When `y = x` is executed, both `x` and `y` point to the same list object in memory. The `y.append(4)` method modifies that shared list in-place, so printing `x` reflects the change and outputs `[1, 2, 3, 4]`.

Answer analysis

Option-by-option breakdown

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

  • ✓

    [1,2,3,4]

    Why this is correct

    Assignment binds y to the same list object as x, not a copy. Calling append(4) mutates that shared object in place, so both names observe the change and printing x outputs [1, 2, 3, 4].

  • ✗

    Error

    Why it's wrong here

    Lists are mutable and y = x binds the same object, so append mutates the list both names reference; print(x) outputs [1, 2, 3, 4] without error. The option is tempting because appending to a list while iterating or indexing a non-sequence does raise exceptions, but neither occurs here.

  • ✗

    [4]

    Why it's wrong here

    Appending 4 adds one element to the existing list rather than replacing its contents, so x remains [1, 2, 3, 4]. A single-element [4] result would require rebinding x to a new list, for example x = [4], not mutation through an alias.

  • ✗

    [1,2,3]

    Why it's wrong here

    Because y = x binds the same list object, y.append(4) mutates that shared object, so x becomes [1, 2, 3, 4]. The [1,2,3] output would only occur if y were a copy, such as y = x.copy() or y = x[:].

About these practice questions

One of 482 original PCEP 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 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.