nerdexam
Python_Institute

PCEP-30-02 · Question #3

What is the output of the following snippet? ``python tup = (1, ) + (1, ) tup = tup + tup print(len(tup)) ``

The correct answer is B. 4. Option B (4) is correct because tuple concatenation with + creates a new tuple containing all elements from both operands. After line 1, tup = (1,) + (1,) produces (1, 1) - a 2-element tuple. Line 2 then concatenates that 2-element tuple with itself, yielding (1, 1, 1, 1), so…

Question

What is the output of the following snippet?
tup = (1, ) + (1, )
tup = tup + tup
print(len(tup))

Options

  • A2
  • B4
  • CThe snippet is erroneous (invalid syntax)

How the community answered

(50 responses)
  • A
    20% (10)
  • B
    72% (36)
  • C
    8% (4)

Explanation

Option B (4) is correct because tuple concatenation with + creates a new tuple containing all elements from both operands. After line 1, tup = (1,) + (1,) produces (1, 1) - a 2-element tuple. Line 2 then concatenates that 2-element tuple with itself, yielding (1, 1, 1, 1), so len(tup) is 4.

Why A (2) is wrong: It only accounts for the first concatenation and ignores the second tup = tup + tup step, which doubles the length again.

Why C is wrong: The syntax is entirely valid Python - the trailing comma in (1,) is the correct way to create a single-element tuple (without it, (1) would just be the integer 1 in parentheses, not a tuple).

Memory tip: Think of + on tuples like appending lists - it always produces a new tuple whose length equals the sum of both sides. So tup + tup always doubles len(tup), regardless of what's inside.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice