350-401 · Question #696
350-401 Question #696: Real Exam Question with Answer & Explanation
The correct answer is C: OutFile = open("devices.json", "w") json.dump(Devices, OutFile) OutFile.close(). Option C is correct because it properly uses json.dump() (not json.dumps()) to serialize the Devices data structure directly into a file object. The json.dump() function takes two arguments: the object to serialize and the file object to write to, which is exactly what OutFile pr
Question
Which Python snippet should be used to store the devices data structure in a JSON file? A. B. C. D.
Options
- Awith open("devices.json", "w") as OutFile: json.dumps(Devices)
- BOutFile = open("devices.json", "w") OutFile.writestr(Devices)) OutFile.close()
- COutFile = open("devices.json", "w") json.dump(Devices, OutFile) OutFile.close()
- Dwith open("devices.json", "w") as OutFile: Devices = json.load(OutFile)
Explanation
Option C is correct because it properly uses json.dump() (not json.dumps()) to serialize the Devices data structure directly into a file object. The json.dump() function takes two arguments: the object to serialize and the file object to write to, which is exactly what OutFile provides after being opened in write mode ('w'). The file is also properly closed after the write operation.
Topics
Community Discussion
No community discussion yet for this question.