LearnFrenzy
Sign in Take a free test

Python Programming · Topic 20 of 25

Iterators and Generators Questions and Concepts

  • 4 exercises
  • 50 questions
  • ~60 min
  • Answers explained

Interview Questions

Iterators and Generators · Study material

Iterators and Generators – Interview Questions

1. What is the difference between an iterable and an iterator?

An iterable can produce an iterator (has __iter__); an iterator produces values one by one (has __next__) and remembers its position. Iterators are iterables that return themselves.

2. What does next() do?

Returns the next item from an iterator, or raises StopIteration (or returns a supplied default) when exhausted.

3. How does a for loop work internally?

It calls iter() on the iterable, then next() repeatedly until StopIteration, which it handles silently.

4. What is a generator?

An iterator created by a function containing yield (or by a generator expression), producing values lazily.

5. Difference between yield and return?

return ends the function with one value; yield produces a value and pauses, so the function can resume and produce more.

6. Why are generators memory-efficient?

They produce one value at a time instead of building the whole sequence in memory.

7. Can you iterate a generator twice?

No; once exhausted it yields nothing. Create a new generator or store the values in a list.

8. What does yield from do?

Yields every value of another iterable or generator, and returns that generator's return value.

9. What happens to a return value in a generator?

It is attached to StopIteration as .value.

10. What does send() do?

Resumes the generator and makes the current yield expression evaluate to the sent value.

11. How do you write an iterator class?

Define __iter__ returning self and __next__ returning the next value or raising StopIteration.

12. What is PEP 479?

Since Python 3.7, a StopIteration raised inside a generator is converted to RuntimeError, so bugs do not silently end iteration.

13. How do you take the first n items of an infinite generator?

itertools.islice(gen, n), or a loop with a counter and break.

14. Are range objects iterators?

No; range is an iterable sequence. iter(range(3)) gives an iterator, and range supports len() and indexing.

15. Name some useful itertools functions.

count, cycle, repeat, islice, chain, takewhile, dropwhile, groupby, accumulate, product, permutations, combinations.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Python Programming

Why it matters

  • Python is the most popular first language and the default for data science, AI, automation and scripting
  • Campus coding rounds and technical interviews increasingly allow – or prefer – Python
  • "Predict the output" questions on lists, strings, dictionaries and mutable defaults are a favourite in written tests
  • Clean Python with comprehensions, generators and OOP is what product companies expect from freshers

Who it is for

  • School students (Class 11–12 computer science with Python) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, coding tests and Python interviews
  • Anyone moving into data science, AI or automation who needs strong Python fundamentals

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs: syntax errors, runtime exceptions and logical bugs
  • Coding problems with solutions and real output, every one run on Python 3 – open any example in the online Python compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture tests
  • Python interviews for freshers
  • Data science interviews
  • CBSE / university practicals
  • GATE CS

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, errors, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links