Find the output of the following Python Code: Assume records.dat contains…

2026

Find the output of the following Python Code:
Assume records.dat contains three pickled integers: 10, 20 and 30.

import pickle

def UpdateBinary():
# Read all into a list
Data = []
with open("records.dat", "rb") as F:
try:
while True:
Data.append(pickle.load(F))
except EOFError:
pass
# Update logic
Data[1] = Data[1] * 5
with open("records.dat", "wb") as F:
pickle.dump(Data[1], F)
with open("records.dat", "rb") as F:
FinalVal = pickle.load(F)
F.seek(0, 2)
Size = F.tell()
print(Data[0])
print(FinalVal)
print(len(Data))
print(Size > 0)

UpdateBinary()

Show answer & explanation
  • File initially contains three pickled integers: 10, 20, 30.

  • Loop with pickle.load() reads all values into list ⇒ Data = [10, 20, 30].

  • Data[1] = Data[1] * 520 × 5 = 100 ⇒ updated list [10, 100, 30].

  • File is reopened in "wb" mode ⇒ previous data erased, only 100 is stored.

  • Reading again ⇒ FinalVal = 100

  • F.seek(0,2) and tell() ⇒ file size > 0 ⇒ True

  • len(Data)3

  • Final Output:

    10  
    100  
    3  
    True

Explore the full course: Hpsc Pgt Computer Science