wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

LECTURE 05 Quizs (25.11.2025)

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

What does the 'r' mode signify when opening a file in Python?

a)

Write mode

b)

Read mode

c)

Append mode

d)

Binary mode

2.

Which Python object allows you to read CSV files as dictionaries?

a)

csv.reader

b)

csv.DictReader

c)

csv.writer

d)

csv.DictWriter

3.

Which method would you use with a `Path` object to check if a directory exists?

a)

exists()

b)

is_dir()

c)

is_file()

d)

mkdir()

4.

Write a Python code snippet that safely reads a file and ensures it is closed even if an exception occurs. Test your code in a compiler.

a)

```python try: with open('example.txt', 'r') as f: print(f.read()) except Exception as e: print("Error:", e) ```

b)

```python with open('example.txt', 'r') as f: print(f.read()) raise Exception("Error!") ```

c)

```python open('example.txt', 'r') print(f.read()) ```

d)

```python try: open('example.txt', 'r') print(f.read()) except Exception as e: print("Error:", e) ```

5.

Write a Python code snippet to read each row of a CSV file as a dictionary using `csv.DictReader`. Test your code in a compiler and check the output.

a)

```python import csv with open('file.csv', 'r') as f: reader = csv.DictReader(f) for row in reader: print(row) ```

b)

```python import csv with open('file.csv', 'r') as f: reader = csv.reader(f) for row in reader: print(row) ```

c)

```python import csv with open('file.csv', 'w') as f: reader = csv.DictReader(f) for row in reader: print(row) ```

d)

```python import csv reader = csv.DictReader('file.csv') for row in reader: print(row) ```

6.

True or False: Using the `with open()` statement in Python ensures that the file is closed automatically after the block is executed.

a)

True

b)

False

7.

Write a Python code snippet that demonstrates how a context manager ensures a file is closed even if an error occurs. Test it in a compiler and observe the behavior.

a)

```python try: with open('test.txt', 'r') as f: raise Exception("Error!") except Exception as e: print("Caught exception:", e) # File is closed automatically ```

b)

```python with open('test.txt', 'r') as f: raise Exception("Error!") # File is not closed ```

c)

```python open('test.txt', 'r') raise Exception("Error!") # File is closed automatically ```

d)

```python try: open('test.txt', 'r') raise Exception("Error!") except Exception as e: print("Caught exception:", e) # File is closed automatically ```

8.

Write a Python code snippet to demonstrate how `pathlib` improves code readability and maintainability for cross-platform file path operations compared to `os.path`. Test your code and explain your reasoning.

a)

```python from pathlib import Path p = Path('folder') / 'file.txt' print(p) # pathlib uses objects and operators, making code more readable and cross-platform. ```

b)

```python import os p = os.path.join('folder', 'file.txt') print(p) # os.path is more readable and modern. ```

c)

```python from pathlib import Path p = Path('folder', 'file.txt') print(p) # pathlib is only for Windows. ```

d)

```python import os p = os.path.join('folder', 'file.txt') print(p) # os.path is preferred for maintainability. ```

9.

Which module is recommended for modern, object-oriented file and directory path handling in Python?

a)

os.path

b)

pathlib

c)

shutil

d)

sys

10.

True or False: The `csv.DictReader` object reads each row of a CSV file as a dictionary, using the column headers as keys.

a)

True

b)

False

11.

Write a Python code snippet that opens a file without a context manager and explain the potential issues. Then, rewrite the code using a context manager to fix the problem. Test both in a compiler.

a)

```python # Without context manager f = open('test.txt', 'r') print(f.read()) # Potential issue: file may not be closed properly. # With context manager with open('test.txt', 'r') as f: print(f.read()) # File is closed automatically. ```

b)

```python # Without context manager open('test.txt', 'r') print(f.read()) # No issues. # With context manager with open('test.txt', 'r') as f: print(f.read()) # No difference. ```

c)

```python # Without context manager f = open('test.txt', 'r') print(f.read()) # File is encrypted. # With context manager with open('test.txt', 'r') as f: print(f.read()) # File is encrypted. ```

d)

```python # Without context manager f = open('test.txt', 'r') print(f.read()) # File is deleted. # With context manager with open('test.txt', 'r') as f: print(f.read()) # File is deleted. ```

12.

Write a Python code snippet to process each row of a CSV file as a dictionary and explain why using `csv.DictReader` is beneficial. Test your code and reasoning.

a)

```python import csv with open('data.csv', 'r') as f: reader = csv.DictReader(f) for row in reader: print(row) # DictReader maps column headers to keys, making access easier. ```

b)

```python import csv with open('data.csv', 'r') as f: reader = csv.reader(f) for row in reader: print(row) # DictReader is slower than reader. ```

c)

```python import csv reader = csv.DictReader('data.csv') for row in reader: print(row) # DictReader is not recommended. ```

d)

```python import csv with open('data.csv', 'w') as f: reader = csv.DictReader(f) for row in reader: print(row) # DictReader is for writing only. ```

13.

Which of the following is NOT a benefit of using a context manager for file operations?

a)

Automatic resource cleanup

b)

Cross-platform path handling

c)

Error handling

d)

Preventing resource leaks

14.

True or False: The `pathlib` module is designed for cross-platform file path operations in Python.

a)

True

b)

False

15.

Which of the following is a standard practice to guarantee that file resources are safely released after use?

a)

try...finally block

b)

os.close()

c)

with open(...) (Context Manager)

d)

Using pathlib.Path

16.

True or False: If you open a file with `open()` but forget to call `close()`, the operating system will always immediately free the resource.

a)

True

b)

False

17.

Write a Python code snippet using a context manager to open a file named "settings.json" for reading and print its contents. Run your code in a Python compiler, then check the answer below.

a)

```python with open('settings.json', 'r') as f: print(f.read()) ```

b)

```python open('settings.json', 'r') print(f.read()) ```

c)

```python with open('settings.json', 'w') as f: print(f.read()) ```

d)

```python open('settings.json', 'w') print(f.read()) ```

18.

Write a Python code snippet using `pathlib` to create a `Path` object for a file named "data.csv". Test it in a Python compiler and verify the result.

a)

```python from pathlib import Path p = Path('data.csv') print(p) ```

b)

```python import os p = os.path('data.csv') print(p) ```

c)

```python from pathlib import Path p = pathlib.Path('data.csv') print(p) ```

d)

```python p = file.Path('data.csv') print(p) ```

19.

Write a Python code snippet using `pathlib` to iterate through all ".txt" files in a directory and print their contents safely. Test your code in a compiler.

a)

```python from pathlib import Path for file in Path('.').glob('*.txt'): with open(file, 'r') as f: print(f.read()) ```

b)

```python import os for file in os.listdir('.'): if file.endswith('.txt'): open(file, 'r') print(f.read()) ```

c)

```python from pathlib import Path for file in Path('.').glob('*.txt'): print(file.read()) ```

d)

```python import shutil for file in shutil.copy('.'): if file.endswith('.txt'): print(file) ```

20.

Write a Python code snippet using `pathlib` to check if a directory named "my_folder" exists. Test it in a compiler and verify the result.

a)

```python from pathlib import Path p = Path('my_folder') print(p.is_dir()) ```

b)

```python from pathlib import Path p = Path('my_folder') print(p.exists()) ```

c)

```python import os print(os.path.isdir('my_folder')) ```

d)

```python from pathlib import Path p = Path('my_folder') print(p.is_file()) ```