Arrange the following in correct order:
A. listvalues=['abc', 'Gen', 1, 2.4]
B. pickle.load(fileobject)
C. fileobject = open("file.dat","wb")
D. pickle.dump(listvalues, fileobject)
Arrange the following in correct order:
A. listvalues=['abc', 'Gen', 1, 2.4]
B. pickle.load(fileobject)
C. fileobject = open("file.dat","wb")
D. pickle.dump(listvalues, fileobject)
Solution
A. listvalues=['abc', 'Gen', 1, 2.4] creates the data to be stored.
C. fileobject = open("file.dat","wb") opens the file in binary write mode, which pickle needs.
D. pickle.dump(listvalues, fileobject) writes the list into the file.
B. pickle.load(fileobject) reads the stored data back, so it can only come after the data has been written.
Options 1 and 2: pickle.load comes before pickle.dump, so there is nothing in the file to read yet.
Option 3: pickle.dump comes before the file is opened and before the list exists.
In real code, close the file after dumping and reopen it in "rb" mode before calling pickle.load.
Related questions:
2026: 26 May Shift 1
2026: 21 May Shift 1