Python Generators & Decorators MCQ Test: 200 Interview Questions With Answers

← All Python MCQ topics and your readiness dashboard

Test your Iterators, Generators and Decorators knowledge with a timed Python MCQ mock test for interviews. Each attempt gives you 20 random questions in 20 minutes from a 200-question bank covering the iterator protocol, single-use iterators, generator functions and expressions, yield from, send, close and throw, itertools, decorators with and without arguments, functools.wraps, lru_cache and singledispatch, class decorators, @contextmanager, async basics, and pytest yield fixtures, markers, retry and step decorators and lazy test data. 52 questions are written from a QA and test-automation angle and the rest from a developer angle; your result shows both scores.

What this Python MCQ test covers

Revise these lessons from the free Python tutorial before or after your test:

Sample Python Generators & Decorators questions with answers

  • Q. Retrying a flaky step with a decorator. What does this code print?
    import functools
    def retry(times=3, exceptions=(Exception,)):
    def deco(fn):
    @functools.wraps(fn)
    def wrapper(*a, **k):
    for attempt in range(1, times + 1):
    try:
    return fn(*a, **k)
    except exceptions:
    if attempt == times:
    raise
    return wrapper
    return deco
    state = {"n": 0}
    @retry(times=3, exceptions=(ConnectionError,))
    def call():
    state["n"] += 1
    if state["n"] < 3:
    raise ConnectionError
    return "ok"
    print(call(), state["n"])

    Answer: ok 3
    The decorator retries only the listed transient errors and re-raises after the last attempt.
  • Q. Retry gives up after the last attempt. What does this code print?
    import functools
    def retry(times):
    def deco(fn):
    @functools.wraps(fn)
    def wrapper():
    for i in range(times):
    try:
    return fn()
    except TimeoutError:
    if i == times - 1:
    raise
    return wrapper
    return deco
    @retry(2)
    def always_slow():
    raise TimeoutError("still loading")
    try:
    always_slow()
    except TimeoutError as e:
    print("failed:", e)

    Answer: failed: still loading
    After the final attempt the real error reaches the test, so the failure is not hidden.
  • Q. Logging every test step. What does this code print?
    import functools
    steps = []
    def step(fn):
    @functools.wraps(fn)
    def wrapper(*a):
    steps.append(fn.__name__.replace("_", " "))
    return fn(*a)
    return wrapper
    @step
    def open_login(): pass
    @step
    def submit_form(): pass
    open_login(); submit_form()
    print(steps)

    Answer: ['open login', 'submit form']
    A step decorator records readable step names for reports.
  • Q. What does this code print?
    it = iter([1, 2, 3])
    print(next(it), next(it))

    Answer: 1 2
    iter() returns an iterator; next() takes the next item.
  • Q. What does this code print?
    it = iter([1])
    next(it)
    try:
    next(it)
    except StopIteration:
    print("done")

    Answer: done
    An exhausted iterator raises StopIteration.
  • Q. What does this code print?
    it = iter("ab")
    print(next(it, "-"), next(it, "-"), next(it, "-"))

    Answer: a b -
    A default for next() avoids StopIteration.

Every predict-the-output question in this bank was run on Python 3, so the answer is the real output. Go back to the Python MCQ dashboard to see your overall Python learning progress and interview readiness.

Frequently asked questions

How many questions does the Python Generators & Decorators MCQ test have?

The bank has 200 questions. Each test picks 20 at random, favouring questions you have not seen yet, so about 10 attempts cover the whole bank.

How long is each test and what is the pass mark?

Each test has a 20-minute timer and submits itself when time runs out. You pass with 70%, which is 14 of 20 correct.

Is this test useful for QA and automation engineer interviews?

Yes. 52 of the questions use test-automation scenarios such as parsing UI text, test data and assertions, and your result shows your QA score separately.

Can I retake the test?

Yes, as often as you like. Every retake draws a fresh set of 20 questions and counts toward your 20-test readiness target.