Quiz: Chapter 7 :Python Data Structures (Python for Everybody Specialization) Answers 2025
-
Given the architecture… where are files stored?
❌ Central Processor
❌ Main Memory
❌ Motherboard
✅ Secondary memory
Explanation: Files live on secondary storage (disk/SSD)—persistent storage outside RAM.
-
What is stored in a “file handle” returned from
open()?
❌ The handle has a list of all files in a folder
❌ All the file data is read into the handle
✅ The handle is a connection to the file’s data
❌ The handle contains the first 10 lines of a file
Explanation: A file handle (file object) is a reference/connection you use to read from or write to the file; it does not contain the whole file automatically.
-
What do we use the second parameter of
open()to indicate?
❌ How large we expect the file to be
❌ List of folders to search
✅ Whether we want to read data from the file or write data to the file
❌ What disk drive the file is stored on
Explanation: The mode string ('r', 'w', 'a', 'rb', etc.) tells Python whether to read, write, append, binary, etc.
-
Which Python function prompts the user for a file name to open?
❌ file_input()
❌ cin
❌ read()
✅ input()
Explanation: input() reads a line from the user (a filename string you can pass to open()).
-
Purpose of the newline character in text files?
✅ It indicates the end of one line of text and the beginning of another line of text
❌ It allows synchronized reading of multiple files
❌ It adds a new network connection
❌ It enables random movement throughout the file
Explanation: \n marks line boundaries in plain text files.
-
If
xfile = open('mbox.txt'), what statement reads the file one line at a time?
❌while (<xfile>) {
❌while ((line = xfile.readLine()) != null) {
❌while line = xfile.gets
✅for line in xfile:
Explanation: In Python the idiomatic way is for line in fhandle: to iterate line-by-line lazily.
-
Purpose of this code that counts
x = x + 1in a loop overfhand:
❌ Convert lines to lower case
❌ Remove leading/trailing spaces
✅ Count the lines in the file ‘mbox.txt’
❌ Reverse the order of the lines
Explanation: The code increments a counter for each line read—so it counts lines.
-
If you see extra blank lines when printing file lines, which function likely solves it?
❌ split()
❌ trim()
✅ rstrip()
❌ ljust()
Explanation: Printed lines already include the trailing \n; print() adds another newline. Use line.rstrip() to remove the trailing newline (and other trailing whitespace) before printing.
-
How to avoid traceback when
open(fname)fails on bad filename?
❌ signal handlers
❌ setjmp / longjmp
❌ try / catch / finally
✅ try / except
Explanation: Wrap open() in try: and handle except FileNotFoundError: (or generic except:) to print a friendly error instead of trace.
-
What does
fhand.read()do?
✅ Reads the entire file into the variableinpas a string
❌ Turns text into a graphic image
❌ Prompts the user for a file name
❌ Checks if file exists and can be written
Explanation: .read() returns the whole file contents as one string (beware memory for large files).
🧾 Summary Table
| Q | Correct answer (short) | Key concept |
|---|---|---|
| 1 | Secondary memory | Files persist on disk/SSD (not RAM) |
| 2 | File handle = connection | File object used to read/write, not full contents |
| 3 | Mode (read/write) | open(filename, mode) selects read/write/append |
| 4 | input() |
Get filename string from user |
| 5 | newline marks line breaks | \n separates lines in text files |
| 6 | for line in xfile: |
Iterate file line-by-line |
| 7 | Count lines | Increment counter per line read |
| 8 | rstrip() |
Remove trailing \n to avoid double blank lines |
| 9 | try / except |
Catch open() errors, print custom message |
| 10 | fhand.read() reads entire file |
Returns full file as one string |