AP CSP topic 2.2
Data Compression
Compression reduces the number of bits needed to store or send data. Lossless compression lets you rebuild the original data exactly, which matters for text and code. Lossy compression throws away some detail to make files much smaller, which works well for photos, music, and video where small losses are hard to notice. Choosing between them is a tradeoff between file size and quality.
Data Compression vocabulary
- Data compression: Reducing the number of bits used to represent data.
- Lossless: Compression that allows the original data to be restored exactly.
- Lossy: Compression that permanently removes some data to save more space.
- Run-length encoding: A lossless method that replaces repeated values with a count, such as AAAA becoming 4A.
AP-style practice questions
Try each one, then open the answer. To save your progress, take this quiz in the interactive course.
1. A student needs to send a copy of their program's source code to a teacher using as few bits as possible. Which type of compression should be used?
- (A) Lossy, because it gives the smallest file
- (B) Lossless, because every character of the code must be restored exactly
- (C) Lossy, because code has many repeated words
- (D) No compression is possible for text
Show answer
(B) Lossless, because every character of the code must be restored exactly Code must be restored exactly, so lossless compression is required.
2. Using run-length encoding, the string "AAABBBBCC" is stored as "3A4B2C". Which of the following is the encoding of "WWWWWWBB"?
- (A) 6W2B
- (B) W6B2
- (C) 2W6B
- (D) 8WB
Show answer
(A) 6W2B Six Ws, then two Bs: 6W2B.
3. A photo-sharing app shrinks images so they upload faster. Users rarely notice the difference in quality. Which of the following best describes this compression?
- (A) Lossless, since nothing changes
- (B) Lossy, since some detail is removed to reduce size
- (C) Encryption, since the image is scrambled
- (D) Redundancy, since copies are stored
Show answer
(B) Lossy, since some detail is removed to reduce size Removing hard-to-notice detail to save space is lossy compression.
4. Which of the following is a tradeoff of lossy compression compared with lossless compression?
- (A) Lossy files are larger but higher quality
- (B) Lossy files are smaller but the original cannot be fully restored
- (C) Lossy files can always be restored exactly
- (D) Lossy compression only works on text
Show answer
(B) Lossy files are smaller but the original cannot be fully restored Lossy saves more space but permanently loses some data.