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]
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
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.
Related questions:
2025: 14 May Shift 1
2025: 14 May Shift 1