Data Structures & File Handling Flashcards
7 cards from real POC practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Data Structures & File Handling flashcards as text
What does `collections.defaultdict(list)` do when a missing key is accessed?
Answer: Automatically creates an empty list for that key
`defaultdict` calls the factory function (`list`) to generate a default value for any missing key, avoiding KeyError.
Which method flushes the internal write buffer to the OS without closing the file?
Answer: f.flush()
`f.flush()` forces Python's internal I/O buffer to be written to the operating system immediately, without closing the file.
What is the output of `{1, 2, 3} & {2, 3, 4}`?
Answer: {2, 3}
The `&` operator computes the intersection of two sets, returning only elements present in both.
When reading a CSV file with Python's `csv.reader`, what type does each row come back as?
Answer: A list of strings
`csv.reader` yields each row as a list of strings, where each element corresponds to a field in that row.
Which statement correctly inserts value `99` at index 2 of list `L`?
Answer: L.insert(2, 99)
`list.insert(i, x)` inserts element `x` before the item at index `i`, shifting subsequent elements right.
What does `dict.items()` return in Python 3?
Answer: A view object of (key, value) pairs
In Python 3, `dict.items()` returns a dynamic view object of `(key, value)` tuples that reflects dictionary changes in real time.
What happens when you open a file with mode `'a'` and write to it?
Answer: Data is appended to the end of the file
Append mode (`'a'`) positions the write pointer at the end of the file, so new data is added after existing content.