PCEP-30-02 · Question #47
What is the expected output of the following code? data = [1, 2, 3, 4, 5, 6] for i in range(1, 6): data[i - 1] = data[i] for i in range(0, 6): print(data[i], end='')
The correct answer is C. 234566. Option C is correct because the first loop shifts every element one position to the left by assigning data[i-1] = data[i], transforming [1,2,3,4,5,6] into [2,3,4,5,6,6] - the last element 6 is duplicated because nothing overwrites index 5. Option A (112345) is wrong because…
Question
Options
- A112345
- B234561
- C234566
- D123456
How the community answered
(43 responses)- A16% (7)
- B7% (3)
- C74% (32)
- D2% (1)
Explanation
Option C is correct because the first loop shifts every element one position to the left by assigning data[i-1] = data[i], transforming [1,2,3,4,5,6] into [2,3,4,5,6,6] - the last element 6 is duplicated because nothing overwrites index 5. Option A (112345) is wrong because that would require a right-shift, not left. Option B (234561) is wrong because it implies a rotation where 1 wraps around to the end, but the loop never touches data[5] as a destination, so 6 remains. Option D (123456) is wrong because changes definitely occur - the original array is modified in-place.
Memory tip: Think of the loop as a conveyor belt sliding elements left - whatever was on the far left falls off, and the far right slot just clones its neighbor since nothing fills it from beyond the array.
Community Discussion
No community discussion yet for this question.