Python Basic Viva Related Q&A for Senior Backend Developer
1. Python Fundamentals
• What are Python’s key features, and why is it popular?
• Explain the difference between deepcopy and shallowcopy.
• How does Python handle memory management?
• What is the difference between a list, tuple, and set?
• Explain mutable vs immutable objects with examples.
2. Object-Oriented Programming (OOP)
• What are the four pillars of OOP in Python?
• How does method overriding work? Provide an example.
• Explain the use of super() in Python.
• What is the difference between a classmethod and a staticmethod?
• How do you implement encapsulation in Python?
3. Python Libraries and Modules
• How do you create and import a Python module?
• What is the purpose of the os and sys modules?
• How do you handle package dependencies in Python?
• Name a few popular Python libraries you’ve used and their purposes.
4. Python Data Structures
• How is a dictionary different from a list?
• How would you implement a queue and a stack in Python?
• Explain the concept of a generator and how it is used.
• What is a comprehension in Python? Provide examples for list, set, and dictionary
comprehensions.
5. Exception Handling
• What is the difference between try...except and finally?
• How can you create a custom exception in Python?
• Explain the use of the raise keyword.
6. File Handling
• How do you read and write files in Python?
• What is the difference between binary and text files?
• Explain the use of the with statement in file handling.
7. Advanced Python Concepts
• Explain Python’s Global Interpreter Lock (GIL).
• What are decorators, and how are they used?
• What is the purpose of the @property decorator?
• Explain the difference between multithreading and multiprocessing in Python.
• What is a context manager? Provide an example.
8. Python Frameworks
• How does Django differ from Flask?
• Explain the purpose of middleware in Django.
• What is WSGI, and why is it important?
9. APIs and Web Development
• How do you create RESTful APIs in Python?
• Explain how you handle authentication and authorization in APIs.
• What are HTTP status codes, and when are they used?
10. Database and ORM
• How do you connect to a database in Python?
• What is an ORM, and why is it used?
• Explain the difference between SQL and NoSQL databases.
11. Testing and Debugging
• What tools have you used for testing in Python?
• How do you debug Python applications?
• Explain the purpose of unittest and pytest.
12. Deployment and DevOps
• What is Docker, and how do you use it for Python applications?
• How do you manage dependencies in a Python project?
• Explain how you deploy a Python application to a production server.
13. Coding Challenges
• Reverse a string without using built-in functions.
• Write a function to check if a string is a palindrome.
• How would you sort a dictionary by its values?
• Write code to implement a singleton class in Python.
Tips for Preparation:
• Brush up on core Python concepts and commonly used libraries.
• Be prepared to explain your past projects and the role you played.
• Practice solving Python coding problems on platforms like LeetCode or HackerRank.
• Familiarize yourself with the tech stack used by the company.
1. Python Fundamentals
Q: What are Python’s key features, and why is it popular?
• A: Python is popular because it’s easy to learn, has a simple syntax, and supports multiple
programming pattern (OOP, functional, procedural). dynamically typed, platform-
independent, and having extensive libraries.
Q: Explain the difference between deepcopy and shallowcopy.
• A: A shallow copy copies the object but not the nested objects (references are shared). A
deep copy copies the object along with all its nested objects. Example: copy.copy() for
shallow, copy.deepcopy() for deep.
Q: How does Python handle memory management?
• A: Python uses automatic memory management through reference counting and a garbage
collector to reclaim unused memory.
Q: What is the difference between a list, tuple, and set?
• A: A list is mutable and ordered. A tuple is immutable and ordered. A set is unordered,
mutable, and stores unique elements.
Q: Explain mutable vs immutable objects with examples.
• A: Mutable objects (like lists) can be changed after creation:
Example: lst = [1, 2]; lst[0] = 3.
Immutable objects (like tuples) cannot:
Example: tup = (1, 2); tup[0] = 3 (will throw an error).
Object-Oriented Programming (OOP)
Q: What is oops?
Oops that uses classes and objects to create reusable code that models real-world concepts
Q: What are the four pillars of OOP in Python?
Abstraction: Hiding implementation details, showing only essential features.
Encapsulation: Restricting direct access to data by bundling it with methods.
Inheritance: Reusing and extending functionality of parent classes in child classes.
Polymorphism: Allowing objects to behave differently based on their class.
Q: How does method overriding work? Provide an example.
• A: A child class redefines a method from the parent class.
class Parent:
def greet(self):
print("Hello from Parent")
class Child(Parent):
def greet(self):
print("Hello from Child")
obj = Child()
obj.greet() # Output: Hello from Child
Q: Explain the use of super() in Python.
• A: super() calls a method from the parent class in a child class.
Example: super().__init__() initializes the parent class constructor.
Q: What is the difference between a classmethod and a staticmethod?
• A: A classmethod uses @classmethod and can access the class via cls. A staticmethod uses
@staticmethod and does not access the class or instance.
Q: How do you implement encapsulation in Python?
• A: Use private variables (_var or __var) and define getter and setter methods.
3. Python Libraries and Modules
Q: How do you create and import a Python module?
• A: Save a Python file as module_name.py and use import module_name to import it.
Q: What is the purpose of the os and sys modules?
• A: os handles file and directory operations. sys deals with system-specific parameters and
functions.
Q: How do you handle package dependencies in Python?
• A: Use a requirements.txt file and manage dependencies with tools like pip or virtualenv.
Q: Name a few popular Python libraries you’ve used and their purposes.
• A: NumPy (numerical computations), Pandas (data analysis), Django (web development), and
Requests (HTTP requests).
4. Python Data Structures
Q: How is a dictionary different from a list?
• A: A dictionary is key-value based and unordered, while a list is index-based and ordered.
Q: How would you implement a queue and a stack in Python?
• A:
o Queue: Use collections.deque.
o Stack: Use a list with append() and pop().
Q: Explain the concept of a generator and how it is used.
• A: Generators use yield to produce values lazily, saving memory.
Example:
def gen():
yield 1
yield 2
**
1. Python Fundamentals
Q: What are Python’s key features, and why is it popular?
• A: Python is easy to learn, interpreted, dynamically typed, and has vast libraries. It’s popular
due to simplicity and versatility.
Q: Explain the difference between deepcopy and shallowcopy.
• A: Shallow copy copies references; deepcopy copies the entire object, including nested
ones.
Q: How does Python handle memory management?
• A: Python uses reference counting and garbage collection for memory management.
Q: What is the difference between a list, tuple, and set?
• A: List is ordered and mutable, tuple is ordered and immutable, set is unordered and stores
unique elements.
Q: Explain mutable vs immutable objects with examples.
• A: Mutable (list) can be changed, immutable (tuple) cannot be changed after creation.
2. Object-Oriented Programming (OOP)
Q: What are the four pillars of OOP in Python?
• A: Encapsulation, Inheritance, Polymorphism, Abstraction.
Q: How does method overriding work? Provide an example.
• A: Child class redefines parent class method.
Example:
class Child(Parent):
def greet(self):
print("Hello from Child")
Q: Explain the use of super() in Python.
• A: super() calls the parent class’s method from a child class.
Q: What is the difference between a classmethod and a staticmethod?
• A: classmethod takes cls, staticmethod doesn’t take self or cls.
Q: How do you implement encapsulation in Python?
• A: Use private variables and getter/setter methods.
3. Python Libraries and Modules
Q: How do you create and import a Python module?
• A: Create a .py file and use import module_name.
Q: What is the purpose of the os and sys modules?
• A: os handles file operations, sys handles system-specific functions.
Q: How do you handle package dependencies in Python?
• A: Use requirements.txt and pip.
Q: Name a few popular Python libraries you’ve used and their purposes.
• A: NumPy (math), Pandas (data), Django (web), Requests (HTTP).
4. Python Data Structures
Q: How is a dictionary different from a list?
• A: Dictionary is key-value, unordered. List is indexed, ordered.
Q: How would you implement a queue and a stack in Python?
• A: Use collections.deque for Queue, list with append()/pop() for Stack.
Q: Explain the concept of a generator and how it is used.
• A: Generators use yield to lazily return values, saving memory.
Q: What is a comprehension in Python? Provide examples.
• A: A concise way to create lists/sets/dictionaries.
Example:
[x for x in range(5)] # List comprehension
5. Exception Handling
Q: What is the difference between try...except and finally?
• A: try...except handles exceptions, finally executes code regardless of exceptions.
Q: How can you create a custom exception in Python?
• A: Define a new class inheriting from Exception.
Q: Explain the use of the raise keyword.
• A: raise is used to trigger an exception manually.
6. File Handling
Q: How do you read and write files in Python?
• A: Use open() with modes 'r' for reading and 'w' for writing.
Q: What is the difference between binary and text files?
• A: Binary files store raw data, text files store human-readable text.
Q: Explain the use of the with statement in file handling.
• A: with ensures proper closing of files after use.
7. Advanced Python Concepts
Q: Explain Python’s Global Interpreter Lock (GIL).
• A: GIL restricts execution to one thread at a time in CPython, limiting multi-core
performance.
Q: What are decorators, and how are they used?
• A: Decorators modify or extend functions.
Example:
@decorator
def func():
pass
Q: What is the purpose of the @property decorator?
• A: It allows getter and setter methods to be used as attributes.
Q: Explain the difference between multithreading and multiprocessing in Python.
• A: Multithreading shares memory space, multiprocessing uses separate memory for each
process.
Q: What is a context manager? Provide an example.
• A: A context manager handles setup and teardown (e.g., file handling).
Example:
with open('file.txt', 'r') as f:
data = f.read()
8. Python Frameworks
Q: How does Django differ from Flask?
• A: Django is full-stack, includes many built-in features. Flask is minimal and more flexible.
Q: Explain the purpose of middleware in Django.
• A: Middleware processes requests globally before reaching views.
Q: What is WSGI, and why is it important?
• A: WSGI is a specification for web servers and Python web apps to communicate.
9. APIs and Web Development
Q: How do you create RESTful APIs in Python?
• A: Use frameworks like Flask or Django REST Framework to build APIs.
Q: Explain how you handle authentication and authorization in APIs.
• A: Use JWT, OAuth, or session-based tokens for authentication and authorization.
Q: What are HTTP status codes, and when are they used?
• A: HTTP status codes indicate the outcome of a request, e.g., 200 OK, 404 Not Found.
10. Database and ORM
Q: How do you connect to a database in Python?
• A: Use libraries like sqlite3, SQLAlchemy, or psycopg2.
Q: What is an ORM, and why is it used?
• A: ORM (Object-Relational Mapper) allows interaction with databases using Python objects.
Q: Explain the difference between SQL and NoSQL databases.
• A: SQL is relational, NoSQL is non-relational, flexible, and schema-less.
11. Testing and Debugging
Q: What tools have you used for testing in Python?
• A: unittest, pytest.
Q: How do you debug Python applications?
• A: Use pdb, print statements, or IDE debuggers.
Q: Explain the purpose of unittest and pytest.
• A: Both are testing frameworks. unittest is built-in, pytest offers advanced features and
flexibility.
12. Deployment and DevOps
Q: What is Docker, and how do you use it for Python applications?
• A: Docker creates containerized environments for running Python apps consistently across
platforms.
Q: How do you manage dependencies in a Python project?
• A: Use virtualenv and pip, with dependencies listed in requirements.txt.
Q: Explain how you deploy a Python application to a production server.
• A: Use tools like Docker, CI/CD pipelines, or platforms like AWS or Heroku.
13. Coding Challenges
Q: Reverse a string without using built-in functions.
• A:
python
Copy code
def reverse_string(s):
return s[::-1]
Q: Write a function to check if a string is a palindrome.
• A:
python
Copy code
def is_palindrome(s):
return s == s[::-1]
Q: How would you sort a dictionary by its values?
• A:
python
Copy code
sorted_dict = sorted(my_dict.items(), key=lambda x: x[1])
Q: Write code to implement a singleton class in Python.
• A:
python
Copy code
class Singleton:
_instance = None
def __new__(cls):
if not cls._instance:
cls._instance = super().__new__(cls)
return cls._instance