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 the `collections.deque` class offer that a regular list does not?
Answer: O(1) append and pop from both ends
`deque` provides O(1) time complexity for appending and popping from both the left and right ends, while list pop(0) is O(n).
Which file mode string opens a binary file for both reading and writing without truncating it?
Answer: rb+
`rb+` opens a binary file for both reading and writing, preserving existing content, while `wb` truncates the file.
Given `d = {'a': 1, 'b': 2}`, what does `d.get('c', 0)` return?
Answer: 0
`dict.get(key, default)` returns the default value (0 here) when the key is not found, instead of raising KeyError.
What is the result of `[1, 2, 3] + [4, 5]` in Python?
Answer: [1, 2, 3, 4, 5]
The `+` operator on lists performs concatenation, returning a new list with all elements from both lists.
When using `with open('file.txt') as f:`, what happens to the file when the block exits?
Answer: It is automatically closed
The context manager protocol calls `f.__exit__()`, which closes the file automatically even if an exception occurs.
What does `set.discard(x)` do differently from `set.remove(x)`?
Answer: It does not raise an error if x is not present
`discard()` silently ignores missing elements, while `remove()` raises a `KeyError` if the element is not found.
Which method reads all lines of a text file into a list of strings?
Answer: readlines()
`readlines()` reads the entire file and returns each line as a string element in a list, including newline characters.