JSON, CSV & pathlib — Concept Check
Questions on JSON, CSV & pathlib. json.load/dump round-trip Python dicts to the web's data format, csv handles tabular files safely, and pathlib replaces string paths with a clean object API.
Questions in this quiz
8 questions on JSON, CSV & pathlib — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have a Python dictionary with non-string keys. What happens when you use json.dumps on it?
- AAn error is raised because keys must be strings.
- BThe keys are converted to strings.
- CThe keys are converted to integers.
- DThe keys are removed from the output.
2.A developer uses csv.DictReader to read a CSV file with quoted fields. What issue might arise if they manually split lines by commas?
- AQuoted fields may be split incorrectly.
- BThe CSV module will automatically handle it.
- CThe file will be read faster.
- DThe CSV module will ignore quoted fields.
3.You want to store a Python datetime object in JSON. What must you do before using json.dumps?
- ADirectly serialize it with json.dumps.
- BConvert it to a string using .isoformat().
- CStore it as a timestamp integer.
- DConvert it to a list of date components.
4.A file path is constructed using pathlib.Path on Windows. How does it behave on Linux?
- AThe path must be manually adjusted for Linux.
- BIt throws an error on Linux.
- CPath objects are only compatible with Windows.
- DIt behaves identically on both platforms.
5.A developer wants to pretty-print a JSON string. Which parameter should they use with json.dumps?
- Apretty
- Bindent
- Cformat
- Dalign
6.What happens if you try to use json.loads on a malformed JSON string?
- AThe function returns None.
- BA JSONDecodeError is raised.
- CThe function fixes the JSON and loads it.
- DThe string is returned as-is.
7.A developer uses Path.glob('*.txt') to find text files. What does this method return?
- AA single Path object.
- BA list of file names.
- CAn iterator of Path objects.
- DA dictionary of file paths.
8.What is a key advantage of using pathlib over traditional string paths in Python?
- AString paths are faster to process.
- BPathlib automatically fixes broken paths.
- CPathlib only works on Windows.
- DPathlib provides an object-oriented API for path operations.