Completed
Binary Files - pickle module
Questions From This Challenge
Q1mcq1 mark
Which module is used in Python to serialize (convert to a byte stream) and deserialize Python objects for binary file storage?
csv
pickle ✓
os
sys
Q2mcq1 mark
Which mode should be used to open a file for writing binary data using pickle.dump()?
"w"
"wb" ✓
"r"
"a" only
Q3mcq1 mark
Which function is used to write a Python object into an already-opened binary file using pickle?
pickle.load()
pickle.dump() ✓
pickle.write()
pickle.save()
Q4mcq1 mark
Which function is used to read back a Python object from a binary file using pickle?
pickle.load() ✓
pickle.read()
pickle.open()
pickle.get()
Q5mcq1 mark
What happens if pickle.load() is called on a binary file after all previously stored objects have already been read (i.e., the pointer is at the end of the file)?
It returns None
It raises EOFError ✓
It returns an empty list
It restarts reading from the beginning
Q6mcq1 mark
Which mode opens a binary file for both reading and writing, without erasing existing content, with the pointer positioned at the start, and requires the file to already exist?
"rb+" ✓
"wb+"
"ab+"
"wb"
Q7mcq1 mark
To add a new pickled record to an existing binary file without disturbing the previously stored records, which mode should be used to open the file?
"wb"
"rb"
"ab" ✓
"r+"
Q8output1 mark
What is the output?
import pickle
data = {"name": "Aisha", "marks": 92}
with open("s1.dat", "wb") as f:
pickle.dump(data, f)
with open("s1.dat", "rb") as f:
loaded = pickle.load(f)
print(loaded)
Correct output:
{'name': 'Aisha', 'marks': 92}Q9output1 mark
What is the output?
import pickle
with open("s2.dat", "wb") as f:
pickle.dump(10, f)
pickle.dump(20, f)
pickle.dump(30, f)
with open("s2.dat", "rb") as f:
a = pickle.load(f)
b = pickle.load(f)
c = pickle.load(f)
print(a + b + c)
Correct output:
60Q10output1 mark
What is the output?
import pickle
nums = [5, 10, 15, 20]
with open("s3.dat", "wb") as f:
pickle.dump(nums, f)
with open("s3.dat", "rb") as f:
loaded = pickle.load(f)
total = 0
for n in loaded:
total = total + n
print(total)
Correct output:
50Q11output1 mark
What is the output?
import pickle
with open("s4.dat", "wb") as f:
pickle.dump("only one record", f)
with open("s4.dat", "rb") as f:
print(pickle.load(f))
try:
print(pickle.load(f))
except EOFError:
print("No more data")
Correct output:
only one record
No more dataQ12output1 mark
What is the output?
import pickle
with open("s5.dat", "wb") as f:
pickle.dump("first", f)
with open("s5.dat", "ab") as f:
pickle.dump("second", f)
with open("s5.dat", "rb") as f:
print(pickle.load(f))
print(pickle.load(f))
Correct output:
first
secondQ13output1 mark
What is the output?
import pickle
def buildRecord(name, age):
record = {"name": name, "age": age}
return record
with open("s6.dat", "wb") as f:
pickle.dump(buildRecord("Rohan", 17), f)
with open("s6.dat", "rb") as f:
print(pickle.load(f))
Correct output:
{'name': 'Rohan', 'age': 17}Q14output1 mark
What is the output?
import pickle
try:
with open("nofile999.dat", "rb") as f:
data = pickle.load(f)
print(data)
except FileNotFoundError:
print("Binary file not found")
Correct output:
Binary file not foundQ15output1 mark
What is the output?
import pickle
students = [{"name": "Zoya", "marks": 88}, {"name": "Kabir", "marks": 76}]
with open("s8.dat", "wb") as f:
pickle.dump(students, f)
with open("s8.dat", "rb") as f:
loaded = pickle.load(f)
for rec in loaded:
print(rec["name"], rec["marks"])
Correct output:
Zoya 88
Kabir 76