Unit 4: Modules and Exception Handling - Subjective Questions

CSR101 — Python Programming • Practice Questions with Detailed Answers

20 questions

1

Explain the purpose of exception handling in Python. Describe the syntax and execution flow of a try and except block with a suitable example.

2

What are multiple exception handlers? Explain how Python selects an appropriate handler when more than one type of exception may occur.

3

Explain how the else clause can be used with exception handling. Compare the roles of try, except, else, and finally.

4

Describe how exceptions are raised explicitly in Python. Explain the use of the raise statement with examples.

5

Explain how exceptions are propagated through functions. Illustrate the difference between handling an exception inside a function and allowing the caller to handle it.

6

Explain the purpose of the finally block. Why is it important for resource cleanup? Provide a suitable example.

7

What are custom exception types? Explain how to define and use a user-defined exception class in Python.

8

Design a Python program that validates a student's marks using a custom exception and handles invalid input gracefully.

9

Define a Python module. Explain the advantages of using modules in program development.

10

Explain how Python finds modules to import. Discuss the role of the current directory, sys.path, and installed packages.

11

Compare the different forms of importing modules in Python, including import module, from module import name, and aliased imports.

12

Explain how to execute a module both as an imported module and as a standalone script. Describe the purpose of if __name__ == "__main__".

13

What is module reloading? Explain when it may be useful and describe one way to reload an already imported module.

14

Define a Python package and explain how packages organize modules. Include an example package structure and an import statement.

15

Explain the Python standard library and discuss the uses of any five commonly used standard-library modules.

16

Describe the datetime module. Explain the difference between date, time, datetime, and timedelta objects with examples.

17

Write a detailed explanation of date formatting and parsing using the datetime module. Include at least four format directives.

18

Design a modular program that reads a date string, calculates an expiry date, and handles invalid date input using exception handling.

19

Explain exception chaining in Python. Why might the raise NewException(...) from original_error syntax be useful?

20

Discuss common principles for writing modular, maintainable, and error-resilient Python code.