Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1f = open('data.bin', 'rb') | AA str is written to a file in 'wb' mode. Binary mode needs bytes, so use b'Hello' or 'Hello'.encode(). | |
| 2f = open('copy.bin', 'wb') | Bf.write(b'AB') | |
| 3Reading exactly 4 bytes from a file opened in binary mode | COpens the file in binary read mode so reading gives raw bytes instead of text | |
| 4Binary data is type bytes, not str, so a b prefix is used for a bytes literal | DThe file is opened in text mode 'r', so read(4) gives 4 characters as str, not 4 raw bytes. Use 'rb'. | |
| 5f = open('data.bin', 'wb') f.write('Hello') | EOpens the file in binary write mode so you can write raw bytes to it | |
| 6f = open('data.bin', 'r') data = f.read(4) | Fchunk = f.read(4) |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
open('data.bin', 'rb') — Opens a file in binary mode so read() returns raw bytes, not text. Use 'wb' to write bytes. Binary data is type bytes, not str, and read(n) reads n bytes.with open('data.bin', 'rb') as f:
data = f.read()
print(type(data))with open('copy.bin', 'wb') as f:
f.write(bytes([65, 66, 67]))with open('data.bin', 'rb') as f:
header = f.read(4)
print(header)with open('data.bin', 'rb') as f:
data = f.read()
print(isinstance(data, bytes))with open('data.bin', 'rb') as f:
chunk = f.read(8)
print(len(chunk))