0% found this document useful (0 votes)
8 views

BSE 224 - python programming QB

The document is a question bank for a Python programming course, divided into two sections: Core Python Concepts and Advanced Python Concepts, each containing 50 questions. Topics covered include data types, functions, object-oriented programming, exception handling, file handling, concurrency, networking, data processing, testing, and deployment. Each question is designed to assess understanding and application of Python programming principles and practices.

Uploaded by

jonathanbusanga1
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)
8 views

BSE 224 - python programming QB

The document is a question bank for a Python programming course, divided into two sections: Core Python Concepts and Advanced Python Concepts, each containing 50 questions. Topics covered include data types, functions, object-oriented programming, exception handling, file handling, concurrency, networking, data processing, testing, and deployment. Each question is designed to assess understanding and application of Python programming principles and practices.

Uploaded by

jonathanbusanga1
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
You are on page 1/ 7

Faculty of Science and Technology

BSE 224 – Python Programming Question Bank

Compulsory Questions

Section A: Core Python Concepts (50 questions)

Data Types and Structures


1. What is the difference between deepcopy and shallow copy in Python? (5 marks)

2. Explain how Python's garbage collection works. (5 marks)

3. What are Python's magic methods and provide three examples with their uses?
(5 marks)

4. How would you implement a custom iterator in Python? Provide code examples.
(10 marks)

5. What is the purpose of the __name__ variable in Python? (5 marks)

6. Differentiate between @staticmethod and @classmethod decorators. (5 marks)

7. How does Python's with statement work and what protocol does it use? (5
marks)

8. What is the difference between __str__ and __repr__ methods? (5 marks)

9. Explain how to create an immutable class in Python. (5 marks)

10. What are Python's context managers and how would you implement one? (5
marks)

Functions and Functional Programming


11. What are lambda functions and when would you use them? (5 marks)

12. Explain the concept of closures in Python with an example. (5 marks)

13. How does Python's *args and **kwargs work? Provide examples. (5 marks)
14. What are decorators in Python and how would you create one? (10 marks)

15. Explain the difference between map(), filter(), and reduce() functions. (5 marks)

16. How would you implement memoization in Python? (5 marks)

17. What are generator functions and how do they differ from normal functions? (5
marks)

18. Explain the concept of currying in Python with an example. (5 marks)

19. How would you handle function composition in Python? (5 marks)

20. What are Python's function annotations and how are they used? (5 marks)

Object-Oriented Programming
21. Explain Python's method resolution order (MRO) with examples. (5 marks)

22. How does multiple inheritance work in Python and what are its pitfalls? (5 marks)

23. What are abstract base classes and how would you implement them? (5 marks)

24. Explain the concept of duck typing in Python. (5 marks)

25. How would you implement operator overloading in Python? (5 marks)

26. What are Python's property decorators and when would you use them? (5 marks)

27. Explain the difference between composition and inheritance in Python. (5 marks)

28. How would you implement the singleton pattern in Python? (5 marks)

29. What are Python's descriptors and how do they work? (5 marks)

30. Explain how to implement polymorphism in Python with examples. (5 marks)

Exception Handling
31. What is the difference between SyntaxError, Exception, and RuntimeError? (5
marks)

32. How would you create a custom exception in Python? (5 marks)

33. Explain Python's try-except-else-finally blocks with examples. (5 marks)

34. What is exception chaining in Python 3? (5 marks)

35. How would you implement retry logic for a failed operation? (5 marks)

36. What is the assert statement used for and when would you use it? (5 marks)

37. Explain how to properly handle resources with exceptions in Python. (5 marks)
38. What are contextlib utilities and how do they help with exception handling? (5
marks)

39. How would you log exceptions in a Python application? (5 marks)

40. What is the difference between raise Exception and raise Exception from cause?
(5 marks)

File Handling and I/O


41. How would you read a large file in Python without loading it entirely into
memory? (5 marks)

42. Explain the difference between text mode and binary mode when opening files.
(5 marks)

43. How would you implement a file-based cache in Python? (5 marks)

44. What are Python's StringIO and BytesIO classes used for? (5 marks)

45. How would you handle CSV files with different encodings? (5 marks)

46. Explain how to work with JSON files in Python. (5 marks)

47. What are Python's tempfile utilities and when would you use them? (5 marks)

48. How would you implement a simple file watcher in Python? (5 marks)

49. Explain how to handle file paths portably across different operating systems. (5
marks)

50. What are the best practices for working with file permissions in Python? (5
marks)

Section B: Advanced Python Concepts (50 questions)

Concurrency and Parallelism


51. What is the Global Interpreter Lock (GIL) and how does it affect Python
programs? (5 marks)

52. Explain the difference between threading and multiprocessing in Python. (5


marks)

53. How would you implement a thread pool in Python? (5 marks)

54. What are Python's coroutines and how do they differ from threads? (5 marks)
55. Explain how asyncio works in Python with examples. (5 marks)

56. How would you handle shared state between threads safely? (5 marks)

57. What are Python's futures and how are they used? (5 marks)

58. Explain the producer-consumer pattern and implement it in Python. (5 marks)

59. How would you implement parallel processing for CPU-bound tasks? (5 marks)

60. What are the common pitfalls when working with threads in Python? (5 marks)

Networking and Web


61. How would you create a simple HTTP server in Python? (5 marks)

62. Explain how to make HTTP requests using the requests library. (5 marks)

63. What are WebSockets and how would you implement them in Python? (5 marks)

64. How would you scrape a website while respecting robots.txt? (5 marks)

65. Explain how to work with REST APIs in Python. (5 marks)

66. What are Python's urllib and urllib2 libraries used for? (5 marks)

67. How would you implement a simple chat server in Python? (5 marks)

68. Explain how to handle cookies and sessions in Python web requests. (5 marks)

69. What are Python's async HTTP clients and when would you use them? (5 marks)

70. How would you implement rate limiting for API requests? (5 marks)

Data Processing and Analysis


71. How would you efficiently process large datasets in Python? (5 marks)

72. Explain the difference between NumPy arrays and Python lists. (5 marks)

73. What are pandas DataFrames and how are they used? (5 marks)

74. How would you handle missing data in pandas? (5 marks)

75. Explain how to perform groupby operations in pandas. (5 marks)

76. What are the advantages of using NumPy over native Python for numerical
computations? (5 marks)

77. How would you implement a moving average in pandas? (5 marks)

78. Explain how to merge and join DataFrames in pandas. (5 marks)


79. What are pandas multi-indexes and when would you use them? (5 marks)

80. How would you optimize memory usage when working with large DataFrames? (5
marks)

Testing and Debugging


81. What are the differences between unittest, pytest, and doctest? (5 marks)

82. How would you implement test fixtures in Python? (5 marks)

83. Explain how to mock objects for testing in Python. (5 marks)

84. What are Python's debugging tools and how would you use them? (5 marks)

85. How would you profile a Python application to find performance bottlenecks? (5
marks)

86. Explain how to implement property-based testing in Python. (5 marks)

87. What are Python's logging best practices? (5 marks)

88. How would you implement continuous integration for a Python project? (5
marks)

89. Explain how to test asynchronous code in Python. (5 marks)

90. What are the common Python anti-patterns to avoid? (5 marks)

Python Ecosystem and Deployment


91. How would you package a Python application for distribution? (5 marks)

92. Explain Python's virtual environments and why they are important. (5 marks)

93. What are the differences between pip, conda, and poetry? (5 marks)

94. How would you deploy a Python web application in production? (5 marks)

95. Explain how to create Python bindings for C libraries. (5 marks)

96. What are Python wheels and why are they beneficial? (5 marks)

97. How would you implement configuration management in a Python application?


(5 marks)

98. Explain how to work with environment variables in Python. (5 marks)

99. What are the best practices for Python project structure? (5 marks)

100. How would you implement feature flags in a Python application? (5


marks)
Answers to Section A: Core Python Concepts

Data Types and Structures

1. Deepcopy vs Shallow Copy:

o Shallow copy creates a new object but doesn't create copies of nested
objects, just references them.

o Deepcopy creates a new object and recursively copies all objects found
in the original.

o Example: copy.copy() vs copy.deepcopy()

2. Python's Garbage Collection:

o Uses reference counting (immediate cleanup when count reaches 0)

o Generational garbage collector for cyclic references

o Can be controlled via gc module

3. Magic Methods:

o __init__: Object initialization

o __add__: Overload + operator

o __iter__: Make object iterable

4. Custom Iterator:

5. __name__ variable:
o Contains the name of the current module

o '__main__' when script is run directly

o Used for if __name__ == '__main__': idiom

6. @staticmethod vs @classmethod:

o Staticmethod: Bound to class, not instance or class - no implicit first


argument

o Classmethod: Bound to class - receives class as first argument

o Staticmethod for utility functions, classmethod for factory methods

7. with statement:

o Implements context manager protocol (__enter__, __exit__)

o Ensures proper resource cleanup

o Used with files, locks, database connections

8. __str__ vs __repr__:

o __str__: Informal, human-readable representation (str(), print())

o __repr__: Official, unambiguous representation (fallback for str())

o repr() should ideally be valid Python code to recreate object

9. Immutable Class:

10. Context Managers:

You might also like