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?
Correct: tuple unpacking swaps values
Why this answer
Option B is correct because 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.
Exam trap
Cisco often tests the distinction between a single-line swap using tuple unpacking and multi-line approaches (like XOR or arithmetic) that technically work but do not satisfy the 'single line' requirement, leading candidates to mistakenly choose those multi-line options.
How to eliminate wrong answers
Option A is wrong because `a = b; b = a` first overwrites `a` with `b`'s value, then assigns the now-overwritten `a` (which equals original `b`) to `b`, resulting in both variables holding the original `b` value, not a swap. Option C is wrong because it uses XOR bitwise operations across three lines, not a single line as required by the question. Option D is wrong because it uses arithmetic operations across three lines, not a single line as required by the question.