Skip to main contentSkip to solution

The following code is used for pickling Python objects. Arrange these code fragments in correct order-

(A). pickle.dump(list, fileobj)

(B). fileobj= open("myfile.dat", "wb")

(C). list= [1, "John", 'f', 25]

(D). fileobj.close( )

Solution

✅ Correct Option: 4

The object to be pickled must exist first, then the file must be opened in binary write mode, then the object is dumped into it, and finally the file is closed.

(C) list= [1, "John", 'f', 25] creates the object

(B) fileobj= open("myfile.dat", "wb") opens the file for binary writing

(A) pickle.dump(list, fileobj) writes the object to the file

(D) fileobj.close( ) closes the file


Option 2: closes the file before dumping, so the write would fail on a closed file.

Pickling needs the file opened in "wb" (binary) mode, because pickle writes bytes, not text.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question