nerdexam
Python_Institute

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

What is the expected output of the following code?
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)
  • A
    4% (2)
  • B
    13% (6)
  • C
    7% (3)
  • D
    76% (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.

Full PCEP-30-02 Practice