PCEP-30-02 · Question #12
What is the expected output of the following code? ``python data1 = '1', '2' data2 = ('3', '4') print(data1 + data2) ``
The correct answer is C. ('1', '2', '3', '4'). Option C is correct because data1 = '1', '2' uses implicit tuple packing - Python treats a comma-separated sequence of values as a tuple even without parentheses, making data1 identical to ('1', '2'). Concatenating two tuples with + always produces a new tuple, so data1 + data2…
Question
data1 = '1', '2'
data2 = ('3', '4')
print(data1 + data2)
Options
- A['1', '2', '3', '4']
- B(1, 2, 3, 4)
- C('1', '2', '3', '4')
- DThe code is erroneous.
How the community answered
(43 responses)- A16% (7)
- B7% (3)
- C74% (32)
- D2% (1)
Explanation
Option C is correct because data1 = '1', '2' uses implicit tuple packing - Python treats a comma-separated sequence of values as a tuple even without parentheses, making data1 identical to ('1', '2'). Concatenating two tuples with + always produces a new tuple, so data1 + data2 yields ('1', '2', '3', '4').
Why the distractors fail:
- A is wrong because square brackets
[]denote a list, not a tuple -+on two tuples never returns a list. - B is wrong on two counts: the parentheses are correct for a tuple, but the values are string literals (
'1','2', etc.), so they print with quotes, not as bare integers. - D is wrong because the code is perfectly valid Python - parentheses are optional when packing a tuple; the comma is what matters.
Memory tip: "The comma makes the tuple, not the parentheses." Whenever you see x = a, b in Python, read it as x = (a, b) - the parentheses are just for clarity, not syntax.
Community Discussion
No community discussion yet for this question.