Find the output of the following Python code. Assume data.bin does not exist…

2026

Find the output of the following Python code. Assume data.bin does not exist initially.

import pickle

def BinaryMysteryIn():
Info = {"A": 100, "B": 200}
Colors = ["Red", "Green"]
# First Write
with open("data.bin", "wb") as F:
pickle.dump(Info, F)
pickle.dump(Colors, F)

def BinaryMysteryOut():
with open("data.bin", "rb") as F:
Obj1 = pickle.load(F)
Obj2 = pickle.load(F)
print(type(Obj1).__name__)
print(Obj2[0])
print(len(Obj1))
try:
pickle.load(F)
except EOFError:
print("EndReached")

BinaryMysteryIn()
BinaryMysteryOut()

Attempted by 15 students.

Show answer & explanation
  • Function BinaryMysteryIn() writes two objects using pickle.dump() ⇒ a dictionary and a list into binary file.

  • BinaryMysteryOut() reads them sequentially using pickle.load().

  • First object (Obj1) is a dictionary ⇒ type(Obj1).__name__dict

  • Second object (Obj2) is a list ⇒ Obj2[0]Red

  • len(Obj1) ⇒ dictionary has 2 keys ⇒ 2

  • Third pickle.load() raises EOFError as file ends, handled by except block.

  • Final Outputs:

    • dict

    • Red

    • 2

    • EndReached

Explore the full course: Hpsc Pgt Computer Science