Unit 6: Files and Exceptions; Regular Expressions - Subjective Questions

INT108 — Python Programming • Practice Questions with Detailed Answers

20 questions

1

Define a text file. Explain how Python represents and processes text-file data.

2

Explain the syntax and purpose of Python's open() function. Describe the commonly used file modes.

3

Describe the different methods used to read data from a text file in Python. Compare read(), readline(), and readlines().

4

Explain how variables of different data types can be written to a text file. Why is conversion sometimes required?

5

Distinguish between writing and appending to a file. Illustrate the difference with Python code.

6

Develop a Python program that copies a text file while numbering its lines. Explain how the program handles resources and errors.

7

Explain how Python programs work with directories and file paths using the pathlib module.

8

What is pickling in Python? Explain how an object is serialized and deserialized.

9

Compare pickling with storing data in a text file. Include the advantages, limitations, and security concerns of pickling.

10

What is an exception? Explain how a ZeroDivisionError occurs and how it can be handled.

11

Describe the structure and execution flow of a try-except statement. How can multiple exception types be handled?

12

Explain the purpose of the else block in exception handling. How is it different from code placed directly after a try-except statement?

13

Explain FileNotFoundError and write a program that handles it while reading a file.

14

Design a robust Python function that divides two user-supplied values and records the result in a file. Handle likely input, arithmetic, and file exceptions.

15

Define a regular expression. Explain the roles of literal characters, metacharacters, and raw strings in Python patterns.

16

Describe the major types of regular-expression elements: character classes, quantifiers, anchors, groups, and alternation. Give examples.

17

Explain the use of re.match(). Compare it with re.search() and re.fullmatch().

18

Construct and explain regular expressions for validating an email address, a ten-digit mobile number, and a date in DD-MM-YYYY format.

19

Explain how regular expressions can be used in web scraping. State the steps involved and the main limitations of this approach.

20

Write and explain a Python program that downloads a web page and extracts email-like strings using regular expressions. Include appropriate error handling.