Python Programming – Interview Questions

Practice interview questions by difficulty level

Showing: All Questions
Q1. What is Python?
Beginner Level
Python is a high-level, interpreted, general-purpose programming language.
Q2. Who developed Python?
Beginner Level
Python was developed by Guido van Rossum.
Q3. Why is Python popular?
Beginner Level
Python is popular because of its simple syntax and wide library support.
Q4. What type of language is Python?
Beginner Level
Python is an interpreted and dynamically typed language.
Q5. What is an interpreter?
Beginner Level
An interpreter executes code line by line.
Q6. What is a variable in Python?
Beginner Level
A variable is a name used to store a value.
Q7. Do we need to declare data types in Python?
Beginner Level
No, Python automatically determines the data type.
Q8. What are Python keywords?
Beginner Level
Keywords are reserved words with predefined meanings in Python.
Q9. What is indentation in Python?
Beginner Level
Indentation defines blocks of code in Python.
Q10. What is a comment in Python?
Beginner Level
Comments are used to explain code and are ignored during execution.
Q11. How do you print output in Python?
Beginner Level
Using the print() function.
Q12. How do you take input in Python?
Beginner Level
Using the input() function.
Q13. What are built-in data types in Python?
Beginner Level
int, float, str, list, tuple, set, and dict are built-in data types.
Q14. What is a list?
Beginner Level
A list is an ordered, mutable collection of items.
Q15. What is a tuple?
Beginner Level
A tuple is an ordered, immutable collection of items.
Q16. What is a set?
Beginner Level
A set is an unordered collection of unique items.
Q17. What is a dictionary?
Beginner Level
A dictionary stores data in key-value pairs.
Q18. What is a string?
Beginner Level
A string is a sequence of characters enclosed in quotes.
Q19. How do you find the length of a string?
Beginner Level
Using the len() function.
Q20. What is type() function?
Beginner Level
type() returns the data type of a variable.
Q21. What is a function?
Beginner Level
A function is a block of reusable code.
Q22. How do you define a function?
Beginner Level
Using the def keyword.
Q23. What is a return statement?
Beginner Level
return sends a value back from a function.
Q24. What is a loop?
Beginner Level
A loop repeats a block of code multiple times.
Q25. Name loop types in Python.
Beginner Level
for and while are loop types in Python.
Q26. What is if statement?
Beginner Level
if executes code when a condition is true.
Q27. What is elif?
Beginner Level
elif checks another condition if previous is false.
Q28. What is else?
Beginner Level
else executes code when all conditions are false.
Q29. What is break statement?
Beginner Level
break exits a loop immediately.
Q30. What is continue statement?
Beginner Level
continue skips the current iteration of a loop.
Q31. What is pass statement?
Beginner Level
pass acts as a placeholder statement.
Q32. What is a module?
Beginner Level
A module is a Python file containing reusable code.
Q33. What is import statement?
Beginner Level
import is used to include modules in a program.
Q34. What is Python pip?
Beginner Level
pip is the package manager for Python.
Q35. What is None?
Beginner Level
None represents absence of a value.
Q36. What is Boolean type?
Beginner Level
Boolean has two values: True and False.
Q37. What is slicing?
Beginner Level
Slicing extracts a portion of a sequence.
Q38. What is range() function?
Beginner Level
range() generates a sequence of numbers.
Q39. What is error in Python?
Beginner Level
An error is a problem that stops program execution.
Q40. What is exception?
Beginner Level
An exception is a runtime error handled using try-except.
Q41. What is Python virtual environment?
Intermediate Level
A virtual environment isolates project dependencies.
Q42. What is PEP 8?
Intermediate Level
PEP 8 is the style guide for writing Python code.
Q43. Difference between list and tuple?
Intermediate Level
Lists are mutable; tuples are immutable.
Q44. What is list comprehension?
Intermediate Level
A concise way to create lists using expressions.
Q45. What is lambda function?
Intermediate Level
An anonymous function defined using lambda keyword.
Q46. What is *args?
Intermediate Level
*args allows passing variable number of positional arguments.
Q47. What is **kwargs?
Intermediate Level
**kwargs allows passing variable number of keyword arguments.
Q48. What is slicing in Python?
Intermediate Level
Extracting a portion of a sequence using indices.
Q49. What is a generator?
Intermediate Level
A generator yields values one at a time using yield.
Q50. Difference between iterator and generator?
Intermediate Level
Generator automatically implements iteration logic.
Q51. What is map() function?
Intermediate Level
map() applies a function to all iterable items.
Q52. What is filter() function?
Intermediate Level
filter() selects items based on a condition.
Q53. What is reduce()?
Intermediate Level
reduce() applies a rolling computation to elements.
Q54. What is decorator?
Intermediate Level
A decorator modifies behavior of a function.
Q55. What is shallow copy?
Intermediate Level
Shallow copy duplicates references of objects.
Q56. What is deep copy?
Intermediate Level
Deep copy duplicates all nested objects.
Q57. What is exception handling?
Intermediate Level
Handling runtime errors using try-except blocks.
Q58. Difference between error and exception?
Intermediate Level
Errors stop execution; exceptions can be handled.
Q59. What is finally block?
Intermediate Level
finally executes code regardless of exception.
Q60. What is a module?
Intermediate Level
A module is a file containing Python definitions.
Q61. What is a package?
Intermediate Level
A package is a collection of Python modules.
Q62. What is __init__.py?
Intermediate Level
It marks a directory as a Python package.
Q63. What is file handling?
Intermediate Level
Reading and writing data to files.
Q64. Difference between read() and readline()?
Intermediate Level
read() reads full file; readline() reads one line.
Q65. What is context manager?
Intermediate Level
Manages resources using with statement.
Q66. What is pickle module?
Intermediate Level
pickle serializes Python objects.
Q67. What is JSON module?
Intermediate Level
json is used to work with JSON data.
Q68. Difference between == and is?
Intermediate Level
== checks value equality; is checks identity.
Q69. What is Python GIL?
Intermediate Level
Global Interpreter Lock allows one thread at a time.
Q70. What is multithreading?
Intermediate Level
Executing multiple threads concurrently.
Q71. What is multiprocessing?
Intermediate Level
Running processes in parallel using multiple CPUs.
Q72. Difference between threading and multiprocessing?
Intermediate Level
Threading shares memory; multiprocessing does not.
Q73. What is regular expression?
Intermediate Level
A pattern used for string matching.
Q74. What is re module?
Intermediate Level
re provides support for regular expressions.
Q75. What is assert statement?
Intermediate Level
assert checks conditions during debugging.
Q76. What is enumerate()?
Intermediate Level
enumerate() returns index and value pairs.
Q77. What is zip()?
Intermediate Level
zip() combines multiple iterables.
Q78. What is sorting in Python?
Intermediate Level
Sorting arranges elements in order.
Q79. Difference between sort() and sorted()?
Intermediate Level
sort() modifies list; sorted() returns new list.
Q80. What is Python memory management?
Advanced Level
Python uses automatic memory management with reference counting and garbage collection.
Q81. What is reference counting?
Advanced Level
It tracks how many references point to an object to manage memory.
Q82. What is garbage collection in Python?
Advanced Level
It removes objects involved in reference cycles.
Q83. What are weak references?
Advanced Level
References that do not increase reference count of an object.
Q84. Explain Python GIL in detail.
Advanced Level
Global Interpreter Lock ensures only one thread executes Python bytecode at a time.
Q85. How does multiprocessing bypass GIL?
Advanced Level
It uses separate processes with independent memory spaces.
Q86. What is asyncio?
Advanced Level
A library for writing concurrent code using async and await.
Q87. Difference between async and threading?
Advanced Level
async uses event loop; threading uses OS threads.
Q88. What is event loop?
Advanced Level
It manages execution of asynchronous tasks.
Q89. What are coroutines?
Advanced Level
Special functions that can pause and resume execution.
Q90. What is metaclass?
Advanced Level
A class that defines behavior of other classes.
Q91. What is __new__ vs __init__?
Advanced Level
__new__ creates object; __init__ initializes it.
Q92. Explain method resolution order (MRO).
Advanced Level
Defines order in which base classes are searched.
Q93. What is diamond problem?
Advanced Level
Ambiguity caused by multiple inheritance hierarchy.
Q94. What is monkey patching?
Advanced Level
Dynamically modifying classes or modules at runtime.
Q95. What is descriptor protocol?
Advanced Level
Defines custom attribute access using __get__, __set__.
Q96. What is __slots__?
Advanced Level
Restricts dynamic attribute creation to save memory.
Q97. What is dataclass?
Advanced Level
A decorator to auto-generate class boilerplate code.
Q98. Difference between shallow and deep copy?
Advanced Level
Shallow copies references; deep copies all nested objects.
Q99. What is serialization?
Advanced Level
Converting object into byte stream for storage or transfer.
Q100. Difference between pickle and JSON?
Advanced Level
pickle handles Python objects; JSON is language-independent.
Q101. What is C extension in Python?
Advanced Level
Extending Python using C for performance.
Q102. What is CPython?
Advanced Level
The reference Python implementation written in C.
Q103. What is PyPy?
Advanced Level
An alternative Python interpreter with JIT compilation.
Q104. What is Just-In-Time compilation?
Advanced Level
Compiling code at runtime for performance.
Q105. What are Python bytecodes?
Advanced Level
Low-level instructions executed by Python VM.
Q106. Explain memory leak in Python.
Advanced Level
Occurs when objects are unintentionally retained.
Q107. What is profiling?
Advanced Level
Measuring program performance and execution time.
Q108. What is cProfile?
Advanced Level
A built-in profiler for Python programs.
Q109. What is contextvars module?
Advanced Level
Manages context-local state in async code.
Q110. What is thread-safe code?
Advanced Level
Code that behaves correctly under concurrent execution.
Q111. What is lock and semaphore?
Advanced Level
Synchronization primitives to control thread access.
Q112. Difference between process and thread?
Advanced Level
Processes have separate memory; threads share memory.
Q113. What is memoryview?
Advanced Level
Provides direct access to object memory without copying.
Q114. What is functools module?
Advanced Level
Provides higher-order function tools.
Q115. What is lru_cache?
Advanced Level
Decorator that caches function results.
Q116. What is Python ABI?
Advanced Level
Defines binary interface between Python and extensions.
Q117. What is sandboxing?
Advanced Level
Restricting execution environment for security.
Q118. How to optimize Python performance?
Advanced Level
Using caching, multiprocessing, and optimized libraries.