OOPs Concepts Python Assessment Guide
OOPs Concepts Python Assessment Guide
1. Introduction to OOP
Object-Oriented Programming is a paradigm based on the concept of "objects", which contain data
and methods.
```python
class Dog:
self.name = name
def bark(self):
d = Dog("Buddy")
print(d.bark())
```
b. Encapsulation
- Hides internal state and requires all interaction to be performed through an object's methods.
```python
class Account:
def __init__(self):
self.__balance += amount
def get_balance(self):
return self.__balance
```
c. Inheritance
- One class can inherit attributes and methods from another class.
```python
class Animal:
def speak(self):
class Dog(Animal):
def speak(self):
```
d. Polymorphism
```python
class Bird:
def fly(self):
class Penguin(Bird):
def fly(self):
def flying_test(bird):
bird.fly()
flying_test(Bird())
flying_test(Penguin())
```
e. Abstraction
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
class Car(Vehicle):
def start(self):
print("Car started")
c = Car()
c.start()
```
```python
class Demo:
def __init__(self):
print("Constructor called")
def __del__(self):
print("Destructor called")
```
b. Class and Static Methods
```python
class MyClass:
count = 0
@classmethod
def increment(cls):
cls.count += 1
@staticmethod
def greet():
print("Hello!")
```
c. Operator Overloading
```python
class Point:
self.x = x
self.y = y
```
c) Overloading operators
2. Output?
```python
class A:
def show(self):
print("A")
class B(A):
def show(self):
print("B")
obj = B()
obj.show()
```
a) A
b) B
c) AB
d) Error
Answer: b) B