Showing posts with label Generators. Show all posts
Showing posts with label Generators. 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

Python Generators & next() Quiz

 Python Generators & next() Quiz


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

   A. 4

   B. 8

   C. 3

   D. 6


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

   A. 12

   B. 20

   C. 9

   D. 15


3. What will be the output? g = (x for x in range(10) if x % 3 == 0) print(next(g) + next(g))

   A. 0

   B. 9

   C. 6

   D. 3


4. What will be the output? g = (x for x in range(5)) a = next(g) b = next(g) print(a * b + next(g))

   A. 3

   B. 2

   C. 0

   D. 1


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

   A. 14

   B. 10

   C. 12

   D. 16