QuizzesThe Iteration Protocol — iter() & next() — Concept Check
intermediate
python

The Iteration Protocol — iter() & next() — Concept Check

Questions on The Iteration Protocol — iter() & next(). Every for loop is sugar over iter() and next(): an iterable produces an iterator, the iterator yields items until StopIteration — implement two dunders and your class joins in.

8 minutes 8 questions Pass at 70%
Time Limit
8 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

8 questions on The Iteration Protocol — iter() & next() — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.You have a list of numbers and create an iterator from it. After consuming all elements using next(), what happens if you call next() again?

    • AIt returns the first element again.
    • BIt resets the iterator.
    • CIt raises a StopIteration exception.
    • DIt returns None.
  2. 2.A developer implements a class with __iter__ but forgets __next__. What happens when you try to iterate over an instance of this class?

    • AIt iterates once and stops.
    • BIt returns an empty sequence.
    • CIt raises a TypeError.
    • DIt iterates indefinitely.
  3. 3.What is the purpose of the StopIteration exception in the iteration protocol?

    • ATo skip an element.
    • BTo reset the iterator.
    • CTo signal the end of iteration.
    • DTo start a new iteration.
  4. 4.You have a zip object created from two lists. After converting it to a list once, what will happen if you do it again?

    • AIt returns the same list again.
    • BIt returns None.
    • CIt raises a StopIteration exception.
    • DIt returns an empty list.
  5. 5.A developer writes a generator function but mistakenly calls it without iterating over it. What does the function return?

    • AThe first yielded value.
    • BA generator object.
    • CNone.
    • DA list of all yielded values.
  6. 6.Which method must an iterable implement to return an iterator?

    • A__getitem__
    • B__next__
    • C__call__
    • D__iter__
  7. 7.What will be the output of calling list() on an already exhausted iterator?

    • AA list with a single None element.
    • BIt raises a StopIteration exception.
    • CThe original list of elements.
    • DAn empty list.
  8. 8.If a class implements both __iter__ and __next__, what can it be considered as?

    • AAn iterator.
    • BNeither an iterable nor an iterator.
    • CAn iterable but not an iterator.
    • DA list.