Tuesday, September 29, 2026

Python file-handling

 ### Top 20 Python `open()` Function Quiz 🐍


Test your Python file-handling knowledge with 20 multiple-choice questions covering file modes, reading, writing, appending, `with` statements, and file pointers.


### Part 1: Basic `open()` Quiz

Q1. What is the default mode of open()?

A. 'w'

B. 'r'

C. 'a'

D. 'x'

Correct answer: B

The default mode is 'r', which opens a file for reading.


Q2. What happens if you open a non-existing file using open('test.txt', 'r')?

A. Creates the file

B. Returns None

C. Raises FileNotFoundError

D. Creates an empty file

Correct answer: C

Read mode requires the file to exist.


Q3. Which mode opens a file for writing and truncates existing content?

A. 'a'

B. 'r'

C. 'w'

D. 'x'

Correct answer: C

'w' creates a file if needed and clears existing content.


Q4. Which mode adds content to the end of a file?

A. 'r'

B. 'a'

C. 'w'

D. 'x'

Correct answer: B

Append mode writes at the end without removing existing content.


Q5. What does mode 'x' do?

A. Reads a file

B. Appends content

C. Creates a new file exclusively

D. Deletes a file

Correct answer: C

'x' raises FileExistsError if the file already exists.


Q6. What does file.read() return?

A. A list of lines

B. A string containing file content

C. A file object

D. An integer

Correct answer: B

read() returns the file contents as a string in text mode.


Q7. What does readline() return?

A. All lines

B. One line, including newline if present

C. A list

D. The last line only

Correct answer: B

readline() reads one line at a time.


Q8. What does readlines() return?

A. A string

B. A tuple

C. A list of lines

D. A dictionary

Correct answer: C

readlines() returns a list where each element is a line.


Q9. Which method writes a string to a file?

A. read()

B. write()

C. readline()

D. append()

Correct answer: B

write() writes a string and does not automatically add a newline.


Q10. What is the purpose of the with statement when opening files?

A. Makes file read-only

B. Automatically closes the file

C. Deletes the file

D. Opens only binary files

Correct answer: B

The context manager closes the file automatically, even if an exception occurs.


### Part 2: Output-Based Quiz

Q11. What is printed if file.txt contains Hello World?

f = open('file.txt', 'r') print(f.read(5))

A. Hello

B. Hello World

C. World

D. Error

Correct answer: A

read(5) reads the first five characters.


Q12. What is printed if file.txt contains Python?

f = open('file.txt', 'r') print(f.read()) print(f.read())

A. Python twice

B. Python followed by a blank line

C. Error

D. None twice

Correct answer: B

The first read consumes the content. The second read returns an empty string.


Q13. What is the content after execution if file.txt initially contains ABC?

f = open('file.txt', 'w') f.write('XYZ') f.close()

A. ABCXYZ

B. ABC

C. XYZ

D. XYZABC

Correct answer: C

Write mode truncates the original file before writing XYZ.


Q14. If file.txt contains Hi, what is its content after this?

f = open('file.txt', 'a') f.write('!') f.close()

A. !

B. Hi!

C. Hi

D. !Hi

Correct answer: B

Append mode adds ! at the end.


Q15. What does this print?

with open('file.txt', 'w') as f: f.write('Python') print(f.closed)

A. False

B. True

C. Python

D. Error

Correct answer: B

The with block closes the file when execution exits the block.


Q16. What is printed?

f = open('file.txt', 'w') print(f.write('Hello')) f.close()

A. Hello

B. 5

C. None

D. True

Correct answer: B


write() returns the number of characters written. Hello has five characters.


Q17. What is printed?

f = open('file.txt', 'w') f.write('ABC\\nDEF') f.close() f = open('file.txt', 'r') print(f.readline().strip())

A. ABC

B. DEF

C. ABC DEF

D. ABC\\nDEF

Correct answer: A

readline() reads the first line; strip() removes the newline.


Q18. What does seek(0) do?

f.seek(0)

A. Closes the file

B. Moves the file pointer to the beginning

C. Deletes the first character

D. Moves to the end

Correct answer: B

seek(0) moves the file pointer to byte/character position zero in ordinary text-file usage.


Q19. What is printed?

f = open('file.txt', 'w+') f.write('ABC') f.seek(0) print(f.read()) f.close()

A. ABC

B. Empty string

C. 3

D. Error

Correct answer: A

w+ permits reading and writing. seek(0) moves the pointer back to the beginning.


Q20. What happens when opening an existing file with mode 'x'?

f = open('existing.txt', 'x')

A. File is overwritten

B. File is appended

C. FileExistsError is raised

D. File is read

Correct answer: C

Exclusive creation fails if the file already exists.


### Quick revision: Python file modes


| Mode | Purpose |

| ---- | ---------------------------------------- |

| r | Read; file must exist |

| w | Write; truncates existing content |

| a | Append; creates file if missing |

| x | Create exclusively; fails i

f file exists |

| r+ | Read and write; file must exist |

| w+ | Read and write; truncates existing file |

| a+ | Read and append; creates if missing |


Remember: Use `with open("file.txt", "r") as f:` for automatic file closing.