Certified Associate in Python Programming (PCAP) — Question 47
You are going to read 16 bytes from a binary file into a bytearray called data. Which lines would you use? (Choose two.)
Answer options
- A. data = bytearray (16) bf.readinto (data)
- B. data = binfile.read (bytearray (16))
- C. bf. readinto (data = bytearray (16))
- D. data = bytearray (binfile.read (16))
Correct answer: A, D
Explanation
Option A correctly initializes a bytearray of size 16 and uses the readinto method to fill it with data from the binary file. Option D also works by reading 16 bytes directly into a new bytearray, which is valid. Options B and C are incorrect because they either attempt to create a bytearray incorrectly or misuse the readinto method.