Unit 3: File Handling and Exception Handling - Subjective Questions

CAP776 — Programming In Python • Practice Questions with Detailed Answers

20 questions

1

What is an exception in Python? Explain the difference between a syntax error, a runtime error, and an exception with suitable examples.

2

Explain the purpose and syntax of the try and except statements in Python. Illustrate your answer with a suitable program.

3

Describe any five built-in exceptions in Python and explain the situations in which each exception is raised.

4

Explain how multiple except blocks are used in Python. Why is it preferable to catch specific exceptions?

5

Explain the use of the else and finally clauses with exception handling. Include a program demonstrating both clauses.

6

What is the purpose of the raise statement in Python? Explain how it can be used to generate a built-in exception.

7

Define a user-defined exception. Explain the steps for creating and using one in Python with an example.

8

Compare built-in exceptions and user-defined exceptions in Python.

9

Explain text file handling in Python. Describe the important file modes and the purpose of the open() function.

10

Explain different methods for reading text files in Python, namely read(), readline(), and readlines().

11

Explain the different ways of writing text to a file in Python using write() and writelines().

12

Why is the with open() statement recommended for file handling? Explain its advantages over explicitly calling close().

13

Write and explain a Python program that copies the contents of one text file into another while handling possible file-related exceptions.

14

Explain how to handle FileNotFoundError, PermissionError, and OSError while working with text files.

15

What is JSON? Explain the relationship between common JSON data types and their corresponding Python data types.

16

Explain how to read a JSON file in Python using the json module. Include a suitable program and describe the result.

17

Differentiate between json.load() and json.loads() in Python with examples.

18

Explain the exceptions that may occur while reading JSON files and describe how they can be handled.

19

Write a Python program that reads a JSON file containing student records and calculates the average marks. Include suitable exception handling.

20

Explain the difference between handling an exception and suppressing an exception. Why should exceptions not be silently ignored?