Skip to content
క
kagitha
/
quiz
/
python-with-generators
Subscribe
Index
⌘K
JAVA NOTEBOOK · QUIZ 30 · INTERMEDIATE
Python with and generators
QUESTION 01 / 04
Which Java feature is Python's with statement closest to?
A
synchronized
B
try-with-resources
C
finalize()
D
static initialisers
SCORE SO FAR · 0 / 0
Share this quiz ↗
Which Java feature is Python's with statement closest to?
— try-with-resources. with runs a block inside a context manager and always cleans up afterwards, even on errors, like try-with-resources closing an AutoCloseable.
What does calling a generator function, like chunks(book, 1000), do immediately?
— Returns a generator object without running any of its code. Nothing runs until someone asks for a value, for example with a for loop. Each request runs the function up to the next yield.
Why use a generator instead of returning a list for a 2 GB file's lines?
— It holds one item at a time in memory and gives the first one right away. A list builds everything in memory before you see anything. A generator produces one value at a time, which keeps memory flat and makes streaming possible.
You loop over a generator twice. What does the second loop do?
— Nothing: the generator is already exhausted. A generator can be consumed only once. Store the values with list(gen) if you need them again.
kagitha
Close
✕
Search everything — articles, videos, chapters, agents, books
01
AI
02
Java
03
System Design
04
Series
05
Manga
06
The Lab
07
Videos
08
Quizzes
09
Books
10
Resume