Skip to main contentSkip to solution

Arrange the statements in order to write the record of a student (roll_no, name, gender and marks) in the binary file named mybinary.dat. (Assume pickle module is imported)

(A) pickle.dump(listvalues,fileobject)

(B) fileobject=open("mybinary.dat", "wb")

(C) fileobject.close()

(D) listvalues=[1,"Geetika",'F', 26]

Solution

✅ Correct Option: 1

The record must exist before it can be written, and the file must be open before dumping and closed after.

(D) listvalues=[1,"Geetika",'F', 26] creates the record

(B) fileobject=open("mybinary.dat", "wb") opens the file in binary write mode

(A) pickle.dump(listvalues,fileobject) writes the record to the file

(C) fileobject.close() closes the file


Option 3: dumps listvalues before it is defined, which gives a NameError.

Options 2 and 4: dump or close appears before the file is opened, so there is no valid file object.

Keyboard Shortcuts

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