Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

Exhibit

funcs = []
for i in range(3):
    funcs.append(lambda: i)

for f in funcs:
    print(f(), end=' ')

Refer to the exhibit. What is the output when the code is executed?

⚠ Common exam trap

The trap is that candidates might assume the loop prints each element of the tuple (0, 1, 2) sequentially, but the code prints the element at index 2 (the third element) each time, resulting in three copies of 2.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

2 2 2

The code defines a tuple `t = (0, 1, 2)`. The `for` loop iterates over the tuple, but each iteration prints `t[2]`, which is the third element (index 2) with value 2. Since the value is constant, it prints '2' three times. Hence the output is '2 2 2'.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    0 1 2

    Why it's wrong here

    The loop's range and body produce different values than 0 1 2; the exhibit's actual iteration count or starting value differs. This sequence is tempting because ascending ranges commonly yield consecutive integers, but the code's specific bounds or reassignment prevent that output.

  • ✗

    0 0 0

    Why it's wrong here

    Repeated zeros would require the loop variable to be reset or never incremented within the body, which the exhibit does not do. This output tempts candidates who assume a print statement inside a loop always emits the initial value rather than the current iteration's value.

  • ✓

    2 2 2

    Why this is correct

    The loop rebinds the same variable each iteration, and the final assignment leaves it holding 2. Printing after the loop completes therefore emits 2 2 2, because all three print calls reference the variable's last value rather than capturing each iteration's value separately.

  • ✗

    3 3 3

    Why it's wrong here

    Three repeated values would require the loop variable to hold its final value throughout, which only happens if printing occurs after the loop completes. The exhibit prints within the loop body, so each iteration emits its own current value instead.

About these practice questions

One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.