PCEP-30-02 · Question #1
What is the expected output of the following code? ``python data = ((1, 2,)) * 7 print(len(data[3:8])) ``
The correct answer is D. 4. There is a likely error in the exam key - the actual Python output is 5 (option C), not 4. Here's the step-by-step breakdown: 1. (1, 2,) - a tuple with 2 elements; the trailing comma is valid and doesn't add an element. 2. ((1, 2,)) - the outer parentheses are grouping only (no…
Question
data = ((1, 2,)) * 7
print(len(data[3:8]))
Options
- AThe code is erroneous.
- B6
- C5
- D4
How the community answered
(20 responses)- A5% (1)
- B20% (4)
- C5% (1)
- D70% (14)
Explanation
There is a likely error in the exam key - the actual Python output is 5 (option C), not 4.
Here's the step-by-step breakdown:
(1, 2,)- a tuple with 2 elements; the trailing comma is valid and doesn't add an element.((1, 2,))- the outer parentheses are grouping only (no trailing comma), so this is still(1, 2).(1, 2) * 7produces a 14-element flat tuple:(1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2).data[3:8]slices indices 3–7 inclusive → 5 elements →len= 5.
Why D (4) is wrong as stated: D would be correct if data had 7 elements. That would only happen if the code used ((1, 2,),) * 7 - a 1-element tuple (tuple-of-tuple) repeated 7 times, giving data[3:8] only 4 reachable indices (3, 4, 5, 6). The exam likely intended that version.
Why other distractors are wrong: A is wrong because the code runs without error. B (6) doesn't correspond to any plausible slice length here. C (5) is actually the correct Python answer.
Memory tip: To create a tuple containing a tuple, you need a trailing comma on the outer: ((1, 2),). Without it, extra parentheses are just grouping - Python ignores them. When you see * N on a tuple, always flatten mentally first, then slice.
Community Discussion
No community discussion yet for this question.