1. What is Object-Oriented Programming (OOP)?
- A programming paradigm based on the concept of objects, which can contain data
and methods. It aims to model real-world entities and relationships.
2. What are the four main principles of OOP?
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
3. What is a class in Python?
- A blueprint for creating objects, defining attributes and methods that the
created objects will have.
4. What is an object in Python?
- An instance of a class that contains data (attributes) and methods to
manipulate that data.
5. How do you define a class in Python?
- Using the `class` keyword followed by the class name and a colon, then
defining attributes and methods within an indented block.
```python
class MyClass:
def __init__(self, value):
[Link] = value
```
6. **How do you create an object in Python?**
- By calling the class name with arguments, if required, to instantiate it.
```python
obj = MyClass(value)
```
7. **What is the `__init__` method in Python?**
- The initializer method (constructor) called when a new instance of a class is
created, used to set initial values for instance attributes.
8. **What are instance variables?**
- Variables defined within a class and unique to each instance of the class.
9. **What are class variables?**
- Variables defined within a class that are shared among all instances of the
class.
10. **What is the difference between instance variables and class variables?**
- Instance variables are specific to each object, while class variables are
shared across all instances of the class.
11. **What is an instance method?**
- A method that operates on an instance of a class and can access instance
variables.
12. **What is a class method?**
- A method that operates on the class itself, rather than an instance, and is
defined using the `@classmethod` decorator.
13. **What is a static method?**
- A method that does not access or modify class or instance state and is
defined using the `@staticmethod` decorator.
14. **How do you define an instance method in Python?**
- By defining a method within a class that takes `self` as its first parameter.
```python
def instance_method(self):
pass
```
15. **How do you define a class method in Python?**
- By using the `@classmethod` decorator and defining the method with `cls` as
its first parameter.
```python
@classmethod
def class_method(cls):
pass
```
16. **How do you define a static method in Python?**
- By using the `@staticmethod` decorator and defining the method without `self`
or `cls`.
```python
@staticmethod
def static_method():
pass
```
17. **What is method overloading?**
- The ability to define multiple methods with the same name but different
parameter lists. (Not supported in Python directly; instead, default arguments or
variable-length arguments are used.)
18. **What is method overriding?**
- Redefining a method in a derived class that already exists in the base class.
19. **What is the purpose of the `super()` function?**
- To call methods from a parent class within a derived class, allowing access
to inherited methods.
20. **How do you override a method in Python?**
- By defining a method in a derived class with the same name as a method in the
base class.
21. **What is inheritance?**
- A mechanism by which one class (derived class) inherits attributes and
methods from another class (base class).
22. **What are the benefits of inheritance?**
- Promotes code reusability, supports hierarchical classification, and allows
for easy maintenance and extension of code.
23. **What is single inheritance?**
- A type of inheritance where a class inherits from a single base class.
24. **What is multiple inheritance?**
- A type of inheritance where a class inherits from more than one base class.
25. **What is a mixin?**
- A class used to provide methods and attributes to other classes through
multiple inheritance, typically not meant to be instantiated on its own.