PCEP-30-02 · Question #9
What is the expected output of the following code? ``python data = {'one': 'two', 'two': 'three', 'three': 'one'} res = data['three'] for _ in range(len(data)): res = data[res] print(res) ``
The correct answer is D. one. Option D is correct because tracing the code reveals a cycle: res starts as data['three'] = 'one', then loops 3 times - 'one'→'two', 'two'→'three', 'three'→'one' - landing back on 'one'. The dictionary forms a closed loop of length 3, and since len(data) is also 3, the loop…
Question
data = {'one': 'two', 'two': 'three', 'three': 'one'}
res = data['three']
for _ in range(len(data)):
res = data[res]
print(res)
Options
- Athree
- B('one', 'two', 'three')
- Ctwo
- Done
How the community answered
(45 responses)- A4% (2)
- B13% (6)
- C7% (3)
- D76% (34)
Explanation
Option D is correct because tracing the code reveals a cycle: res starts as data['three'] = 'one', then loops 3 times - 'one'→'two', 'two'→'three', 'three'→'one' - landing back on 'one'. The dictionary forms a closed loop of length 3, and since len(data) is also 3, the loop completes exactly one full cycle, returning to the starting value.
Option A (three) would be correct if the loop ran 2 iterations instead of 3, stopping one step early. Option C (two) would be the result after just 1 iteration. Option B is wrong because print(res) outputs a string, not a tuple - no tuple is ever constructed in this code.
Memory tip: Think of it as a circular chain - one→two→three→one. Starting at one and taking 3 steps on a 3-element cycle always brings you back to where you started.
Community Discussion
No community discussion yet for this question.