0% found this document useful (0 votes)
4 views4 pages

Python OOP and Exception Handling Interview Questions

The document provides a comprehensive set of interview questions and answers related to Python data structures, object-oriented programming (OOP), and exception handling. It covers key concepts such as lists, tuples, dictionaries, classes, inheritance, and exception management techniques. Each section includes specific questions with concise explanations and examples to aid understanding.

Uploaded by

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

Python OOP and Exception Handling Interview Questions

The document provides a comprehensive set of interview questions and answers related to Python data structures, object-oriented programming (OOP), and exception handling. It covers key concepts such as lists, tuples, dictionaries, classes, inheritance, and exception management techniques. Each section includes specific questions with concise explanations and examples to aid understanding.

Uploaded by

Rinki Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Data Structures, OOP, and

Exception Handling – Interview Q&A


🔹 Data Structures
1. Q: What is a list? How is it different from a tuple?

Answer: List: mutable, ordered collection (e.g., [1, 2, 3])


Tuple: immutable, ordered collection (e.g., (1, 2, 3))

2. Q: How do you create a dictionary in Python?

Answer: Using curly braces:


d = {'name': 'Alice', 'age': 25}

3. Q: What are sets in Python?

Answer: Unordered collection of unique elements.


Example: s = {1, 2, 3}

4. Q: What are comprehensions? (List, dict, set comprehensions)

Answer: - List: [x*x for x in range(5)]


- Dict: {x: x*x for x in range(5)}
- Set: {x*x for x in range(5)}

5. Q: Explain slicing in Python.

Answer: Used to extract a portion of sequences:


Example: list[1:4] returns items from index 1 to 3

6. Q: What is the difference between shallow copy and deep copy?

Answer: - Shallow copy copies the outer object only.


- Deep copy copies both outer and nested objects.
Use copy module:
import copy
copy.deepcopy(obj)

🔹 Object-Oriented Programming (OOP)


7. Q: What are the four pillars of OOP?

Answer: - Encapsulation
- Inheritance
- Polymorphism
- Abstraction

8. Q: How do you create a class in Python?

Answer: class Animal:


def __init__(self, name):
self.name = name

9. Q: What are instance, class, and static methods?

Answer: - Instance: operates on object (uses self)


- Class: operates on class (uses cls, @classmethod)
- Static: general utility method (@staticmethod)

10. Q: What is inheritance? Explain different types.

Answer: Inheritance allows a class to use methods and properties from another.
Types: Single, Multiple, Multilevel, Hierarchical, Hybrid

11. Q: What is method overriding?

Answer: Redefining a parent class method in the child class.

12. Q: What is multiple inheritance? Does Python support it?

Answer: Yes, Python supports multiple inheritance. A class can inherit from more than one
base class.
13. Q: What is encapsulation in Python?

Answer: Binding data and methods together and restricting direct access using _ (protected)
and __ (private).

14. Q: What is polymorphism?

Answer: The ability of different classes to use the same interface. Example: different classes
have the same method name.

15. Q: What is the use of the super() function?

Answer: Used to call the parent class method or constructor.


Example: super().__init__()

16. Q: What is the difference between an object and a class?

Answer: Class is a blueprint; object is an instance of a class.

17. Q: What is the difference between init and new methods?

Answer: __new__ creates the object, __init__ initializes it.

18. Q: What are magic methods or dunder methods? Give examples.

Answer: Special methods with double underscores, e.g., __init__, __str__, __len__, __add__

19. Q: Explain method resolution order (MRO).

Answer: MRO is the order in which base classes are searched when executing a method.
Python uses C3 linearization.
🔹 Exception Handling
20. Q: How does Python handle exceptions?

Answer: Using try-except blocks:


try:
risky_code()
except Exception as e:
print(e)

21. Q: What is the difference between syntax error and exception?

Answer: - Syntax error: error in code structure (e.g., missing colon)


- Exception: runtime error during execution (e.g., ZeroDivisionError)

22. Q: What are try, except, else, and finally blocks?

Answer: - try: write code that may raise exception


- except: handle exception
- else: runs if no exception occurs
- finally: always executes (for cleanup)

23. Q: How do you raise exceptions in Python?

Answer: Use the raise keyword:


raise ValueError('Invalid input')

24. Q: What are custom exceptions? How to create one?

Answer: Define a new class inheriting from Exception:


class MyError(Exception):
pass

25. Q: What is the purpose of the assert statement?

Answer: Used for debugging to test assumptions:


assert x > 0, 'x must be positive'

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy