Showing posts with label Iteration. Show all posts
Showing posts with label Iteration. Show all posts

Thursday, September 10, 2026

Python Generators & Iteration Quiz

 Python Generators & Iteration Quiz


1. What will be the output? g = (x * 2 for x in range(5)) print(next(g)) print(next(g))

   A. 0 then 2

   B. 0 then 1

   C. 2 then 4

   D. 2 then 6


2. What will be the output? g = (x for x in range(6) if x % 2 == 1) print(next(g) + next(g))

   A. 3

   B. 4

   C. 8

   D. 6


3. What will be the output? g = (x + 1 for x in range(4)) print(list(g))

   A. [1, 2, 3, 4]

   B. [1, 2, 3]

   C. [2, 3, 4, 5]

   D. [0, 1, 2, 3]


4. What will be the output? g = (x for x in range(5)) print(next(g)) print(next(g)) print(list(g))

   A. 0, 1, [2, 3, 4]

   B. 1, 2, [3, 4]

   C. 0, 1, [0, 1, 2, 3, 4]

   D. 0, 2, [1, 3, 4]


5. What will be the output? g = (x * x for x in range(1, 6)) print(next(g) + next(g) + next(g))

   A. 30

   B. 9

   C. 14

   D. 16