nerdexam
Python_Institute

PCEP-30-02 · Question #20

What is the expected output of the following code? ``python 1 t1 = (1, 4, 9) 2 t2 = ('A', 'D', 'Z') 3 t3 = (True, False, None) 4 t4 = (5.0, 7.5, 9.9) 5 6 t1, t3 = t2, t4 7 print(t1) ``

The correct answer is C. ('A', 'D', 'Z'). Option C is correct because line 6 uses simultaneous (tuple) assignment: t1, t3 = t2, t4 reassigns t1 to the value of t2 - which is ('A', 'D', 'Z') - so print(t1) outputs that tuple. Option B is the classic trap: it assumes t1 still holds its original value (1, 4, 9)…

Question

What is the expected output of the following code?
1 t1 = (1, 4, 9)
2 t2 = ('A', 'D', 'Z')
3 t3 = (True, False, None)
4 t4 = (5.0, 7.5, 9.9)
5 
6 t1, t3 = t2, t4
7 print(t1)

Options

  • AThe program will cause an error.
  • B(1, 4, 9)
  • C('A', 'D', 'Z')
  • D(5.0, 7.5, 9.9)

How the community answered

(57 responses)
  • A
    5% (3)
  • B
    2% (1)
  • C
    84% (48)
  • D
    9% (5)

Explanation

Option C is correct because line 6 uses simultaneous (tuple) assignment: t1, t3 = t2, t4 reassigns t1 to the value of t2 - which is ('A', 'D', 'Z') - so print(t1) outputs that tuple. Option B is the classic trap: it assumes t1 still holds its original value (1, 4, 9), forgetting that line 6 overwrites it. Option D ((5.0, 7.5, 9.9)) is what t3 receives from this assignment, not t1 - it's the right-hand value but paired with the wrong variable. Option A is wrong because simultaneous assignment is perfectly valid Python syntax and raises no error.

Memory tip: In a, b = x, y, Python evaluates the entire right side first, then assigns left-to-right - think of it as "swap in one breath." Whenever you see reassignment before a print, always trace the latest value, not the initial one.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice