Arrange the following code segment to read a binary file in the correct order.
(A) import pickle
(B) obj=pickle.load(fileobj)
(C) fileobj=open("myfile.dat","rb")
(D) print(obj)
Arrange the following code segment to read a binary file in the correct order.
(A) import pickle
(B) obj=pickle.load(fileobj)
(C) fileobj=open("myfile.dat","rb")
(D) print(obj)
Solution
✅ Correct Option: 2
(A) import pickle
(C) fileobj=open("myfile.dat","rb")
(B) obj=pickle.load(fileobj)
(D) print(obj)
The module must be imported before use, and the file must be opened in "rb" mode before pickle.load() can read from it. The object is printed only after it has been loaded.
Options 1 and 3: pickle.load(fileobj) comes before fileobj is created, which gives a NameError.
Option 4: print(obj) comes before obj is loaded, which also gives a NameError.
Related questions:
2025: 20 May Shift 1
2026: 21 May Shift 1
2025: 22 May Shift 1