0% found this document useful (0 votes)
3 views7 pages

Python Question Bank Unit5

The document is a comprehensive question bank for Python focusing on files, exceptions, modules, and packages. It includes objective questions, short answer questions, and long answer questions covering various aspects of Python programming. The content is structured into three sections, each addressing different levels of understanding and application of Python concepts.

Uploaded by

DHEENATHAYALAN K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Python Question Bank Unit5

The document is a comprehensive question bank for Python focusing on files, exceptions, modules, and packages. It includes objective questions, short answer questions, and long answer questions covering various aspects of Python programming. The content is structured into three sections, each addressing different levels of understanding and application of Python concepts.

Uploaded by

DHEENATHAYALAN K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Question Bank: Unit 5

Files, Exceptions, Modules, and Packages

Section A: Objective Type (1 Mark Each)

Short questions focusing on file modes, exception types, and module basics.

1. What is the default mode when opening a file using `open()`?

2. Which method is used to close an open file object?

3. What does the 'a' mode signify in file handling?

4. Which function is used to read all lines of a file into a list?

5. What is the purpose of the `write()` method?

6. What is the character used for a new line in text files?

7. What does the format operator `%` do with strings?

8. Which module is used to access command line arguments?

9. What is the index of the script name in `[Link]`?

10. Define an 'Exception' in Python.

11. What is the keyword used to handle exceptions?

12. What does the `finally` block do?

13. How do you manually trigger an exception in Python?

14. What is a Python Module?

15. What is a Python Package?

16. Which file is required to make a directory a package in Python (pre-3.3)?

17. What is the result of `open('[Link]', 'r').read()` if the file is empty?

18. What exception is raised when dividing by zero?

19. What is the purpose of the `with` statement in file handling?

20. How do you import only the `pi` constant from the `math` module?
21. What is a 'Syntax Error'?

22. Which exception is raised when a file is not found?

23. What is the difference between `read()` and `readline()`?

24. How do you write a list of strings to a file at once?

25. What does `[Link][1]` typically represent?

26. Can you open a file for both reading and writing simultaneously? (Yes/No)

27. What is the purpose of the `os` module?

28. What keyword is used to catch a specific exception?

29. What is the output of `'Result: %d' % 10.5`?

30. What does the `tell()` method of a file object return?

31. How do you move the file pointer to a specific position?

32. What is a 'Logical Error'?

33. Can a `try` block have multiple `except` blocks? (Yes/No)

34. What is the purpose of the `as` keyword in an `import` statement?

35. What happens to existing data when a file is opened in 'w' mode?

36. What is the base class for all built-in exceptions?

37. How do you check if a file exists using the `os` module?

38. What is an 'AttributeError'?

39. What is the purpose of the `__name__` variable in a module?

40. How do you import all functions from a module?

41. What is a 'Namespace' in the context of modules?

42. Which method checks if a file is connected to a terminal device?

43. What is the result of `1 / 0` inside a `try` block with no `except`?

44. How do you create a directory using Python?

45. What is the use of `[Link]()`?

46. Can you nest `try-except` blocks? (Yes/No)

47. What is the difference between a module and a library?


48. What is a 'TypeError'?

49. What does `[Link]()` do?

50. How do you get the current working directory in Python?

Section B: Short Answer (5 Marks Each)

Conceptual questions on the mechanics of error handling, command-line usage, and package
structures.

1. Explain the different modes of opening a file in Python.

2. Describe the use of the `with` statement for file handling and why it is preferred.

3. How do you read a file line by line using a `for` loop?

4. Explain the 'Format Operator' with examples for different data types.

5. What are Command Line Arguments? Explain how to use `[Link]`.

6. Differentiate between Compile-time errors and Runtime errors.

7. Explain the basic structure of a `try-except` block with an example.

8. What is the purpose of the `else` and `finally` clauses in exception handling?

9. Explain the difference between a Module and a Package.

10. How do you create and use a user-defined module in Python?

11. Describe the steps to create a Python Package with a directory structure.

12. Explain how to handle multiple exceptions using a single `except` block or multiple
blocks.

13. Discuss the `raise` statement and how it is used for custom error signaling.

14. Explain the `write()` vs `writelines()` methods for file objects.

15. How do you use the `[Link]` module to manipulate file paths?

16. Explain the concept of 'Exception Propagation'.

17. Describe how to rename and delete files using the `os` module.

18. What is the significance of `if __name__ == '__main__':` in a module?

19. Explain how to catch and access the error message of an exception.

20. Describe the use of the `pickle` module (briefly) for object serialization.
21. How do you search for a specific string within a text file?

22. Explain the `seek()` and `tell()` methods for random access in files.

23. Discuss the common built-in exceptions: `ValueError`, `TypeError`, and `IndexError`.

24. Explain the difference between absolute and relative paths.

25. How do you handle exceptions in a way that the program continues to run?

26. What is a 'Docstring' in a module and how is it accessed?

27. Explain the `from module import *` syntax and its potential risks.

28. How can you pass a variable number of arguments via the command line?

29. Describe the process of appending data to an existing text file.

30. Explain how to use the `try-finally` construct for resource cleanup.

31. What are 'Standard Streams' (stdin, stdout, stderr) in Python?

32. How do you handle a `FileNotFoundError` gracefully?

33. Explain how modules help in organizing large Python projects.

34. Describe the `importlib` module (briefly) for dynamic imports.

35. How do you list all files in a directory using Python?

36. Explain the difference between `import math` and `from math import sqrt`.

37. Discuss the use of nested packages and how to import from them.

38. How do you read a specific number of characters from a file?

39. Explain how to use the format operator for padding and alignment.

40. Describe the 'Search Path' that Python uses to find modules.

41. How do you create a custom exception class?

42. Explain the role of the `sys` module in system-specific parameters.

43. Describe how to read a CSV file using basic file operations (without csv module).

44. What is 'Monkey Patching' in the context of modules? (Conceptual).

45. Explain the concept of 'Module Shadowing'.

46. How do you capture standard output into a file?

47. Discuss the importance of closing files and the `close()` method.
48. Explain how to use `try-except` inside a loop.

49. How do you check the size of a file in Python?

50. Describe the difference between text mode and binary mode in files.

Section C: Long Answer (8 Marks Each)

Advanced questions requiring detailed programs and theoretical explanations of system-level


interactions.

1. Write a detailed guide on File I/O in Python, covering opening, reading, writing, and
closing with various modes.

2. Discuss Exception Handling in depth: explain the life cycle of an exception from `try` to
`finally`.

3. Explain the architecture of Python Packages. How do they enable modularity in large-
scale applications?

4. Write a Python program that accepts file names as command line arguments and
merges their contents into a new file.

5. Analyze the `sys` and `os` modules. Provide a list of at least 10 functions/attributes from
each and their uses.

6. Create a comprehensive tutorial on creating, importing, and managing custom


Modules in Python.

7. Explain the concept of 'Clean Code' in the context of file handling and exception
management (e.g., Resource Acquisition Is Initialization).

8. Discuss the 'Format Operator' (%) and the `format()` method vs. f-strings for complex
data reporting.

9. Describe how to build a robust application that handles various file-related errors using
nested `try-except` blocks.

10. Explain the internal mechanism of how Python imports a module (search path,
compiled bytecode, etc.).

11. Write a program to count the number of words, lines, and characters in a given text
file, handling all possible exceptions.

12. Discuss 'Custom Exceptions': why they are needed, how to define them, and how to use
them in a business logic layer.
13. Explain the use of 'Context Managers' (`__enter__` and `__exit__`) and their relation to
the `with` statement.

14. Compare 'Look Before You Leap' (LBYL) vs. 'Easier to Ask for Forgiveness than
Permission' (EAFP) in Python programming.

15. Describe the process of packaging a Python project for distribution (briefly mentioning
`[Link]` and PyPI).

16. Discuss the security implications of file handling (e.g., path traversal) and how to
mitigate them.

17. Explain how to use Python to interact with the file system: creating, moving, renaming,
and deleting directories and files.

18. Write a detailed explanation of the `traceback` module and how it helps in debugging
exceptions.

19. Discuss the role of 'Logging' vs. 'Printing' when errors occur in production-level code.

20. Explain the binary file format handling in Python and how it differs from text file
handling.

21. Describe the `argparse` module for creating sophisticated command-line interfaces.

22. Discuss the impact of 'Circular Imports' in Python and how to resolve or avoid them.

23. Explain the 'Global Interpreter Lock' (GIL) in relation to I/O-bound tasks like file reading
(conceptually).

24. Write a program that reads a configuration file (.ini or .txt) and stores settings in a
dictionary.

25. Explain the use of the `tempfile` module for managing temporary files and directories.

26. Describe the hierarchy of Python’s built-in Exception classes and how it affects `except`
block ordering.

27. Discuss the efficiency of different file reading methods (`read()`, `readlines()`, and
iterating) for multi-gigabyte files.

28. Explain how to use the `shutil` module for high-level file operations like copying and
archiving.

29. Describe the workflow of 'Unit Testing' for functions that involve file I/O and
exceptions.

30. Discuss the use of 'Assertions' (`assert` statement) vs. Exceptions for program
validation.
31. Explain the concept of 'Virtual Environments' and their role in managing packages and
dependencies.

32. Describe the process of 'Encoding' and 'Decoding' (UTF-8, ASCII) in the context of text
files.

33. Write a script that automates the organization of files in a directory based on their
extensions.

34. Explain the `warnings` module and how it differs from the `exceptions` mechanism.

35. Discuss the best practices for writing 'Package-level documentation'.

36. Explain how to use `[Link]` to create scripts that work with Unix pipes.

37. Describe the role of `__init__.py` in different Python versions (3.3+ vs. legacy).

38. Discuss how to perform 'Batch Renaming' of files using regular expressions and the
`os` module.

39. Explain the 'Scope' of variables defined inside `try` and `except` blocks.

40. Describe the use of the `glob` module for pattern matching in file names.

41. Write a program that simulates a simple 'Log File Parser' to extract error statistics.

42. Explain how to redirect `[Link]` to a file dynamically during program execution.

43. Discuss the trade-offs between using many small modules vs. one large module.

44. Explain the 'Namespace Packages' concept in Python 3.

45. Describe the use of `zipfile` module to read and write compressed archives.

46. Discuss how Python handles 'File Locking' across different operating systems.

47. Explain the significance of 'Metadata' in files and how to access it (e.g., modification
time).

48. Describe how to build a 'Plugin System' using dynamic module loading.

49. Discuss the evolution of Python's Exception system (e.g., Exception chaining with
`from`).

50. Write a summary of the 'Standard Library' philosophy regarding Modules and
Packages.

You might also like