Courseiva

PCEP Tuple unpacking Practice Question

A developer needs to swap the values of two variables a and b without using a temporary variable. Which single line of code correctly performs the swap?

⚠ Common exam trap

Candidates may think that XOR or arithmetic swaps are acceptable, but they require multiple lines. The question explicitly asks for a 'single line of code', so only tuple unpacking qualifies.

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

✓

a, b = b, a

Python supports tuple unpacking, which allows swapping the values of two variables in a single line without a temporary variable. The expression `a, b = b, a` evaluates the right-hand side tuple `(b, a)` first, then assigns the values to `a` and `b` respectively, effectively swapping them. Option A fails because after `a = b`, the original value of `a` is lost, so `b = a` assigns the same value to both. Option C (XOR swap) works but requires three statements, not a single line. Option D (arithmetic swap) also works but requires three statements and may cause overflow in other languages, though Python handles big integers; still, it is not a single line.

Answer analysis

Option-by-option breakdown

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

  • ✗

    a = b; b = a

    Why it's wrong here

    Assigning a = b overwrites a's original value, so b = a then copies b onto itself, losing the swap. It is tempting as the obvious sequential assignment, but without a temporary variable the first assignment destroys the data needed by the second; tuple assignment or XOR is required instead.

  • ✓

    a, b = b, a

    Why this is correct

    Tuple packing and unpacking evaluates the right-hand side first, building the tuple (b, a), then assigns its elements to a and b. This swaps the values in one statement, satisfying the no-temporary-variable constraint in the stem.

  • ✗

    a = a ^ b; b = a ^ b; a = a ^ b

    Why it's wrong here

    XOR swapping works for integers but fails when a and b are the same variable or non-integer types, and the stem asks for a single line. It is tempting because XOR needs no temporary variable, yet Python's idiomatic a, b = b, a performs the swap in one line for any type.

  • ✗

    a = a + b; b = a - b; a = a - b

    Why it's wrong here

    Arithmetic swapping overflows fixed-width integers and breaks when a and b are the same object, and it spans three statements rather than one line. It is tempting because addition and subtraction need no temporary variable, but Python's a, b = b, a swaps any type in a single line.

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

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.